Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need to execute a perl script from another perl script. In addition I need to pass two variables to the executed script. I would appreciate anyone's input regarding this - I am only learning. Thanks. L. Thompson (Miss) :)
  • Comment on Executing a perl script from a perl script???

Replies are listed 'Best First'.
Re: Executing a perl script from a perl script???
by davorg (Chancellor) on Sep 20, 2001 at 15:13 UTC

    Depending on exactly what you want to do, you'll need qx(), system, exec or fork. There are other options, but those are the most common. Try reading the documentation on those and seeing which fits your needs best.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you don't talk about Perl club."

Re: Executing a perl script from a perl script???
by suaveant (Parson) on Sep 20, 2001 at 17:29 UTC
    if you need the output from the second perl script you run...
    $resp = `script2.pl \Q$var1\E \Q$var2\E`;
    the \Q and \E escape any strange characters in var1 and var2 so they don't screw up the call (like & and ' etc), and script2.pl will see those values in the @ARGV array var1 in $ARGV[0] and var2 in $ARGV[1]... the exit value of script2.pl will be in $?

    if script2.pl puts out a lot of info you may want something more like

    open(CMD, "script2.pl \Q$var1\E \Q$var2\E") or die $!; while(<CMD>) { chomp; print ">>$_\n"; } close CMD;
    that does essentially the same thing as the ``, except you get the output of script2.pl line by line in $_ in the while loop. (chomp removes the trailing \n from $_)

    Finally there is system, which returns the exit code of the script, not the output, the output just goes normally to STDOUT

    if(system("script2.pl \Q$var1\E \Q$var2\E")){ print "Error running script2.pl!\n"; }

                    - Ant
                    - Some of my best work - Fish Dinner

Re: Executing a perl script from a perl script???
by IOrdy (Friar) on Sep 20, 2001 at 17:43 UTC
    Under what circumstances would you want to use a perl script under another perl script when you are 'only learning’?

    If it’s only for the purposes of re-using code then you need to look at why you wouldn’t either a) paste the code into the new project b) maybe read a book on OO so you have an idea of the principals of code reusability and write yourself a module or ‘include’ the code.

    What happens if you change/delete the script you are calling down the track. It could break all sorts of other scripts you have that use the first script.

    Though you can call a script from a script, I think your should just be asking yourself why and should I before you go ahead and do it. You may have a good reason for wanting to do this, but good coding practices would be easier to acquire as you learn not after. That’s my .02 anyway.

    edit: Oh and ++ to all those above/below that are going to give a real answer.
Re: Executing a perl script from a perl script???
by broquaint (Abbot) on Sep 20, 2001 at 17:36 UTC
    One way of doing this would be -
    $cmd = 'script.pl'; $var1 = 'foo'; $var2 = 'bar'; # this executes $cmd with $var[12] as arguments system("perl $cmd $var1 $var2");
    Now in script.pl you could do this
    ($var1, $var2) = @ARGV;
    or this
    $var1 = shift @ARGV; $var2 = shift @ARGV;
    to get the variables you passed over. So as you can see there's are quite a large number of ways of doing this. You may want to go the Getopt::Std route, or perhaps using davorg's suggestion and use fork() instead (which saves you having to pass variables).
    HTH

    broquaint

Re: Executing a perl script from a perl script???
by duckyd (Hermit) on Sep 20, 2001 at 21:45 UTC
    Though you probably don't want to do it, there are many ways to call one script from another. You would execute it the same way you would any other command, using system, exec, `` (backquotes), open(H,"cmd args |"), etc...
    see this for system, you can go from there. hope this helps