Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re: calling a perl script within a perl script

by sk (Curate)
on Nov 27, 2005 at 02:29 UTC ( #511975=note: print w/ replies, xml ) Need Help??


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


Comment on Re: calling a perl script within a perl script
Select or Download Code
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?

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://511975]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (13)
As of 2014-12-18 16:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (58 votes), past polls