Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations John Tel on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

running one perl file .WITH PARAMETERS. from another perl file 1

Status
Not open for further replies.

bcdixit

Technical User
Nov 11, 2005
64
US
Hello,
i want to run one perl file from another and also want to pass parameters to it. how do it do that?

e.g.
lets say I have a file called parent.pl

in main.pl, i have two variables
$x = 'test1';
$y = 'test2';

i want to run a file , lets say child.pl, from main.pl
i did the following
`child.pl -x $test1 -y $test2`;
or used the system() function

My problem is , although the child.pl file runs, $test1 or $test2 dont get passed to child.pl from main.pl

Is there a way to do pass these values?

let me know
thanks
bcd

PS: child.pl is capable of accepting $test1 and $test2 as $opt_x and $opt_y respectively.
 
how do you normally pass arguments to child.pl?

- Kevin, perl coder unexceptional!
 
this is the first time I am trying to do something like this...i am fairly new to perl.

i can normally run child.pl from the command line following way
/home/bcd/child.pl -x abc -y xyz

i want to run the same thing but from parent.pl but want to use $test1 and $test2 as variables and pass them as mentioned previously.

Just wanted to know if there is way to do it
 
I would think this would work then:


Code:
system("child.pl -x $test1 -y $test2");

or try:

Code:
system("child.pl \-x $test1 \-y $test2");






- Kevin, perl coder unexceptional!
 
Unless I am mistaken.. you are passing the wrong variable names to your next script.

You said:
$x = 'test1';
$y = 'test2';
`child.pl -x $test1 -y $test2`;

but what you need is:
$x = 'test1';
$y = 'test2';
`child.pl -x $x -y $y`;
 
>> Unless I am mistaken.. you are passing the wrong variable names to your next script.


DOH! I totally missed that. [flush]

- Kevin, perl coder unexceptional!
 
yeah..sorry it was a mistake on my part...but thanks ..looks like your earlier solution is working
 
It is also possible to use do for this as well. You just need to modify @ARGV explicitly before calling the child script.

parent.pl
Code:
my $x = 'test1';
my $y = 'test2';

my $return = do {
	local @ARGV = ('-x', $x, '-y', $y);
	do 'child.pl';
};

print "return code is '$return'\n";

child.pl
Code:
print "ARGV = @ARGV\n";

output:
Code:
>perl parent.pl
ARGV = -x test1 -y test2
return code is '1'

This has the added benefit that you can add the error checking as detailed in the perldoc about do:

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top