1

My intention was to call a Child.pl script from a Parent.pl script

I used system() function to call other script.
When using

system("perl Child.pl $argument");  

the argument is passed.

But when using

system("Child.pl $argument");

the argument is not passed.

Is that because of any mistake in code.
I am a novice in perl scripting. Pardon me if i am asking any blunder.

I am using windows 7 and my perl version is v5.8.4

My Parent.pl script is

use warnings;

print "Inside Parent Script\n\n";
$var = 10;
print "Calling system without perl prefix\n";
system("Child.pl $var");
print "\nCalling system with perl prefix\n";
system("perl Child.pl $var");
print "\nEnd of Parent Script\n";

exit 0;  

My Child.pl script is

use warnings;

print "\tInside Child Script\n";
print "\tArgument = $ARGV[0] \n";
print "\tEnd of Child Secipt\n";

exit 0;  

My Output is

Inside Parent Script

Calling system without perl prefix
    Inside Child Script
Use of uninitialized value in concatenation (.) or string at C:\Child.pl line 4.
    Argument =  
    End of Child Script

Calling system with perl prefix
    Inside Child Script
    Argument = 10 
    End of Child Script

End of Parent Script

It seems like in some PC its working without perl prefix.
I am dealing with a huge junk of scripts which does not use perl prefix.
Is there any way i can keep those scripts intact and still make the argument pass through.

Borodin
  • 126,100
  • 9
  • 70
  • 144
0xAB1E
  • 721
  • 10
  • 27

1 Answers1

0

First:

Add shebang line in both scripts.

Second:

Give full path of your child script.

Third:

Make sure both scripts are executable.

Parent.pl:

#!/usr/bin/perl

use warnings;

print "Inside Parent Script\n\n";
$var = 10;
print "Calling system without perl prefix\n";
system("./Child.pl $var");
print "\nCalling system with perl prefix\n";
system("perl Child.pl $var");
print "\nEnd of Parent Script\n";

exit 0;

Child.pl:

#!/usr/bin/perl

use warnings;

print "\tInside Child Script\n";
print "\tArgument = $ARGV[0] \n";
print "\tEnd of Child Secipt\n";

exit 0; 

Output:

Inside Parent Script

Calling system without perl prefix
    Inside Child Script
    Argument = 10 
    End of Child Secipt

Calling system with perl prefix
    Inside Child Script
    Argument = 10 
    End of Child Secipt

End of Parent Script

That should work fine for you too.

  • But in some systems its working without any edits. I am dealing with a huge list of scripts and if i have to edit i would have to edit some 300 scripts. Is there any system dependency or something like that. – 0xAB1E Aug 12 '15 at 11:23
  • If you are using Windows then shebang line shoud be something like: #!C:\home\apps\perl\bin\perl.exe -w . If you can post something specific where it is not working, that would be helpful.. – Malli Paritala Aug 12 '15 at 11:33
  • Use of uninitialized value in concatenation (.) or string at C:\Child.pl line 4. is coming when accessing the argument in child script. Without shebang the scripts are running in some machines. So will there be any system settings that will make it run without editing the scripts. – 0xAB1E Aug 12 '15 at 11:46
  • 1
    @MalliParitala: Perl on Windows *ignores* the shebang line, except for the options such as `-w`. In any case `use warnings` is superior, so just omit the shebang line altogethe – Borodin Aug 12 '15 at 12:16