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

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/` $f || die "Error!";

You need to change that to -

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

Also, your  || die is tricky. If your 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 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 '', 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/ $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 ( 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 $f`; and then print out $ret for each file.

      Make sure you have  use warnings

      Hope this helps.


      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 * on the directory you want?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://511975]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2018-06-25 01:35 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.