use strict;
use warnings;
use IPC::System::Simple qw(system capture);
# Run a command, wait until it finishes, and make sure it works.
# Output from this program goes directly to STDOUT, and it can take input
# from your STDIN if required.
system($^X, "yourscript.pl", @ARGS);
#OR
# Run a command, wait until it finishes, and make sure it works.
# The output of this command is captured into $results.
my $results = capture($^X, "yourscript.pl", @ARGS);
# Both of these commands allow an exit value of 0, 1 or 2 to be considered
# a successful execution of the command.
system( [0,1,2], $^X, "yourscript.pl", @ARGS );
# OR
capture( [0,1,2, $^X, "yourscript.pl", @ARGS );
________________________________________________________
I can think of a few ways to do this. You already mentioned the first two, so I won't go into detail on them.
1. backticks: $retVal = `perl somePerlScript.pl `;
2. system() call
3. eval
The eval can be accomplished by slurping the other file into a string (or a list of strings), then 'eval'ing the strings. Heres a sample:
#!/usr/bin/perl
open PERLFILE, "<somePerlScript.pl";
undef $/; # this allows me to slurp the file, ignoring newlines
my $program = <PERLFILE>;
eval $program;
4 . do:
do 'somePerlScript.pl'
________________________________________________________
{
local @ARGV = qw<param1 param2 param3>;
do '/home/buddy/myscript.pl';
}
________________________________________________________
#!/usr/bin/perl
use strict;
open(OUTPUT, "date|") or die "Failed to create process: $!\n";
while (<OUTPUT>)
{
print;
}
close(OUTPUT);
print "Process exited with value " . ($? >> 8) . "\n";
________________________________________________________
If you need to asynchronously call your external script -you just want to launch it and not wait for it to finish-, then :
# On Unix systems, either of these will execute and just carry-on
# You can't collect output that way
`myscript.pl &`;
system ('myscript.pl &');
# On Windows systems the equivalent would be
`start myscript.pl`;
system ('start myscript.pl');
# If you just want to execute another script and terminate the current one
exec ('myscript.pl');
________________________________________________________
If you would like to make a call to, or reference, a Perl script from within a different Perl script, you can accomplish this a system(), exec() or the backtick operator to the path for Perl. For example:
system("/usr/bin/perl /path/to/my_script.pl ");
Or store output to array:
@myarray = `/usr/bin/perl mysecondperlscript.pl`;
`perl /home/com/begp/test_script.pl $f` || die "Error!";
________________________________________________________
#backtrick method
$retVal = `perl somePerlScript.pl`;
print $retVal;
________________________________________________________
No comments:
Post a Comment