http://www.perlmonks.org?node_id=511975


in reply to calling a perl script within a perl script

`perl /home/com/begp/test_script.pl` $f || die "Error!";

You need to change that to -

`perl /home/com/begp/test_script.pl $f` || die "Error!"; # Note the $f position before the backtick.

Also, your  || die is tricky. If your test_script.pl returns a zero then it will die. backticks return something so use it. Also consider using system and check for the return on the system call.

An example of the return problem -

[sk] % cat prifile #!/usr/bin/perl -w print @ARGV; # print whatever is sent. [sk] % cat callprifile #!/usr/bin/perl -w my @files = glob ('*'); push (@files, "0"); # adding a zero as the last element in this array +to demonstrate return issue foreach $f (@files) { `prifile $f` || die "something went wrong with $f\n"; } [sk] % callprifile something went wrong with 0

Replies are listed 'Best First'.
Re^2: calling a perl script within a perl script
by sgifford (Prior) on Nov 27, 2005 at 04:24 UTC
    Also, your || die is tricky. If your test_script.pl returns a zero then it will die.
    It's actually even trickier. Since the backticks will return the output of the script, if the script doesn't output anything, the main program will die. system is definitely better than backticks if you don't care about the output.

    If you care about speed, consider using do 'filename.pl', which will avoid firing up a new Perl interpreter. You may have to set @ARGV manually to make this work.

Re^2: calling a perl script within a perl script
by Anonymous Monk on Nov 27, 2005 at 03:19 UTC
    `perl /home/com/begp/test_script.pl $f` || die "Error!";
    I have changed the positioning of the backtick...but it still errors. Also, the test_script does not return a zero.
      You got to give us more information on the error (can you post the error?). if you are talking about your die erroring out then you you need to check if your program (test_script.pl) worked or not before you debug the one you posted. The code you had and the one i posted work just fine for me (with the modification).

      Also couple of things -

      1. Can you run your test script on the command line with an argument does it work? can you do that with a * (shell wild-card) does it work?

      2. Do something like  my $ret = `perl test_script.pl $f`; and then print out $ret for each file.

      Make sure you have  use warnings

      Hope this helps.

      -SK

      Update: Thanks sgifford! It is a good point and I think that could be the reason why the script is failing.

      sbp, Actually tho, i am wondering why would you even need this wrapper script? Can't you just do test_script.pl * on the directory you want?