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

Hi Monks,

I am calling a perl script thru another script.

My question is whether it is possible to pass values along with called script i.e. Say I am calling 'x.pl'.

Now I want to pass 2 values that will be taken as an input by 'x.pl' and will be used for procsssing with in 'x.pl'.

Thanks for any help

  • Comment on Passing values along with the called script

Replies are listed 'Best First'.
Re: Passing values along with the called script
by shmem (Chancellor) on May 12, 2008 at 09:21 UTC

    Say you have 2 values in the variables $foo and $bar. To run x.pl in another process:

    $foo = 'whatever'; $bar = '42'; system $^X, 'x.pl', $foo, $bar; # run a new "perl script args"

    to call x.pl from within the same perl interpreter:

    $foo = 'whatever'; $bar = '42'; { local @ARGV = ($foo, $bar); do 'x.pl' };

    In both variants $foo and $bar are in @ARGV in x.pl.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      thanks
      Hi Monk,

      Your answer really did, what I wanted.

      Is it possible to return the output back into the calling script.

      Like .. taking the above script as example i.e. 'x.pl' and its two values => $foo, $bar

      Now I have another script lets call it 'y.pl'

      Now from 'y.pl' I am calling 'x.pl' and simultaneously passing these values.

      Then the output of 'x.pl' back to 'y.pl'. Something like 'return' does

      Thanks For any help

Re: Passing values along with the called script
by Corion (Patriarch) on May 12, 2008 at 09:06 UTC

    I'm not really clear what you mean by "input". But have you looked at @ARGV in perlvar already? Or maybe you need to be more explicit in what you want - maybe you want to read the values from STDIN?

Re: Passing values along with the called script
by locked_user sundialsvc4 (Abbot) on May 12, 2008 at 12:57 UTC

    Scripts usually do not “call each other.”

    Why not? Because there's a better way to do it. Build the various common functionality into one or more packages, then create scripts that invoke the routines and objects defined within the packages.

    Once you're in a package-based environment, directives like use and require become readily-available to you. You're operating now in a single process (probably) and therefore the same instance of Perl. Now, you can use variables and other such things quite freely.

    Yes, you can do things another way (TMTOWTDI™ ...), but always look-for what is the best available way in your particular situation. In other words, strive to look objectively at the goal and to consider all of the ways to fill it. Many's the time when I “jumped to a conclusion” and later discovered that I had just “jumped in the lake.” (Ooh, I hate it when that happens!)

Re: Passing values along with the called script
by apl (Monsignor) on May 12, 2008 at 09:33 UTC
    You could always write a "x.pl" that does nothing more than echo it's parameters to answer your own question...