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

Hi - I'm trying to run a perl script that comes from a Database, and pass some parameters to that script. The test script below gives you the main idea. However, I can't find a way to pass params to the SCRIPT. Parameters passed to the RUN command go to PERL, and not to the script. HELP !!!
(This is a Windoze 2000 environment - does it matter?)
I want to pass "SomeText" to the $CalledScript, and expect to see the output "Hello\n Argument=SomeText".
---------------------
#ThisScript use IPC::Run qw( run timeout ) ; my $CalledScript = "print qq(hello\n Argument=\@ARGV[0]\n);"; my $out; my @prog = ("perl"); run \@prog, \$CalledScript, \$out; foreach(split /\n/,$out){ print "$_;\n"; }

Replies are listed 'Best First'.
Re: Passing parameters to child script
by thelenm (Vicar) on Apr 16, 2002 at 15:54 UTC
    You may want to change \@ARGV[0] to $ARGV[0].
      Yes - sorry - I meant that to be \$ARGv[0].
      However, the original question remains - how do I pass parameters to the child script ?
Re: Passing parameters to child script
by Anonymous Monk on Apr 17, 2002 at 20:45 UTC

    Losing my Anaomyity - this was my post, and I have found the right way to do this, and wanted to share it.

    I appealed to a higher authority - the author of the IPC::Run module, and his response follows:


    From: Barrie Slaymaker
    To: Anand, Vijay (WW)
    Cc:

    Subject: Re: Passing parameters to a child script
    Sent: 4/17/02 10:31 AM
    Importance: Normal

    On Wed, Apr 17, 2002 at 09:38:29AM -0700, Anand, Vijay (WW) wrote:
    > My apologies if I'm out of line
    No problem for quick answers :)
    > I'm trying to run a perl script that comes from a Database, and pass some
    > parameters to that script.

    my @prog = ( $^X, "--", "-", @parms );

    will put @parms in the child's @ARGV while executing a script from STDIN:

    - The $^X is instead of your "perl" and will let you test this
    script with other versions of perl if you ever need to. That's
    not usually a problem on Win32; most people just upgrade and hope
    nothing breaks :).
    - The "--" says "here end options for perl".
    - The "-" says "read the script from STDIN".

    This is not tested on Win2K, let me know how it goes there. Note that
    you should read the perlrun page thoroughly if you intend to do much of
    this (it tells you about the "--" and "-" options), and the perlvar page tells you about the $^X.

    Also note that you may need to be careful about passing in any -I options that were passed in to the parent's perl.

    Note that Win2k is really shitty about passing these; IPC::Run has to combine your parameters back in to a single command line and make some educated guesses about what quoting is required / expected by your child process.
    > (This is a Windoze 2000 environment - does it matter?)

    Very much; windows is quite crippled when it comes to launching and managing subprocesses. It was never something that marketing worried about and the windows pipe/file/socket APIs are inconsistent with respect to each other and the command line parameter passing is as sucky as DOS ever was--exactly as sucky. Mind you, Unix has plenty of warts, too, they just don't happen to be in such critical areas of the OS as process management. End of rant. Hope it all works out. - Barrie


    You will be happy to note that the code below works beautifully:
    #ThisScript use IPC::Run qw( run timeout ) ; my $CalledScript = "print qq(hello\n Argument=\$ARGV[0]\n);"; my $out; #my @prog = ("perl"); my @prog = ( $^X, "--", "-", "Passed-Parameter" ); run \@prog, \$CalledScript, \$out; foreach(split /\n/,$out){ print "$_;\n"; } ===== This prints out : hello; Argument=Passed-Parameter;