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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Calling another PERL script with DO & Args and Scalers 3

Status
Not open for further replies.

ljsmith91

Programmer
May 28, 2003
305
US
I am new to PERL so I still have much to learn.

I am trying something simple like calling a second PERL script from another script. I was pointed in the direction of the DO instruction. It works fine except when I dont pass arguments to the called script or when I do not use scalers in the DO statement. But once these are added, I never get the called script.

Here is the one that works fine:

do "called_script.pl";

And this is the one I need that fails consistantly:

$path="e:\srvapps\scripts";
$server="Server201";

do "$path\called_script.pl -M $server";

I have tried so many different combos of this and just cannot get it to run. Anyone know of a good manual that explains this type of syntax issue ? Any help would be great with the immediate problem.

Thanks.

LJS
 
system ($path\called_script.pl -M $server); I think will work.
 
I forgot to mention that I need to capture the output of the Called script so I am not sure if the SYSTEM function is the answer... or is it ?
 
Sure it is. Just do the following:

Code:
my $results - system($path\called_script.pl -M $server);

- Rieekan
 
Hmm, system returns the exit status of a command, not its output. I think you'd need backticks for that.
Code:
my $results = [b]`[/b]$path\called_script.pl -M $server[b]`[/b];
This should give you the output of script.pl in the string $results.

You may need to say `perl $path\called_script.pl -M $server` if script.pl is not an executable.

An alternate strategy would be to direct the output of the script to a file with >, then read the file to get the results. In this case you could use system.

Yet another possibilty: open a pipe from the process and read the output from the pipe, e.g.
Code:
open(PIPEIN, "perl $path\called_script.pl -M $server |") ||
    die qq(Can't open pipe!\n);
while (<PIPEIN>) {
    #read the output
    ...
}
close(PIPEIN);






 
Mikevh,

Thanks for the additional info. It is all very helpful. I am trying it all for best results. You mention directing the output of the script using > and system. Would that be like this?

$outfile = "script.out";
system ($path\called_script.pl -M $server) > $outfile;

As a beginner, I am confused by so many ways to do so many different things.

Thanks again.
 
The > $outfile goes inside the parens as part of the system call.
I think you will need to double the backslash to get a single backslash. Might be better to store the command in a string first, then pass it to system. Then you can easily print the command you've assembled for debugging if necessary, e.g.
Code:
my $cmd = qq($path\\called_script.pl -M $server > $outfile);
my $result = system($cmd);
# $result will have 0 for success, 1 for failure
if ($result) {
    warn qq(Couldn't execute "$cmd"!\n);
    # Take evasive action ...
}
HTH



 
If it appears to be that you will be using this second script often it might be wise to think of rebuilding it into a perl module.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top