Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl: Calling a perl script from another

Tags:

perl

I have a perl script which calls another script. I am calling it using backticks and passing argument to that script and it works fine.

`CQPerl call_script.pl $agr1 $agr2 $arg3`;

But please suggest if there is another better way to do so. How can I check if the script errored out because of the calling script or the script that was called. How do I do that check from the calling script itself?

like image 627
iDev Avatar asked Aug 21 '12 00:08

iDev


2 Answers

If you wan't to do error checking, backticks may be a wrong approach. You probably want to use the system function. See the documentation for all the details of error handling, examples included.

Perl has a number of possibilites to execute other scripts / commands:

  • backticks / qx{} When you want to read all the output at once after the program has terminated
  • exec When you wan't to continue your process as another program — never returns if succesfull
  • system When you are only interested in the success or failure of the command
  • open When you want to pipe information to or from the command
  • do and require Execute another Perl script here. Similar to C's #include
  • There are modules to do a three-way open so that you have access to STDIN, STDOUT and STDERR of the program you executed. See the apropriate parts of perlipc for advanced information.

And always use the multi-argument forms of these calls to avoid shell escaping (can be annoying and very insecure).

like image 161
amon Avatar answered Oct 01 '22 10:10

amon


Check the value of the perl special variable $? to determine if there was an error.

like image 29
David Avatar answered Oct 01 '22 08:10

David