http://www.perlmonks.org?node_id=863406

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

I'm executing one Perl script to another....and I'm trying to pass arguments automatically from one to the other. This doesn't work:
#!/usr/bin/perl use strict; use warnings; my $otherScript = '/usr/local/bin/script'; system ("$otherScript test"); $ARGV[0] = "First"; print $ARGV[0];
I only see it filled when I exit the script. What can I do to pass the arguments? Thanks!

Replies are listed 'Best First'.
Re: Passing arguments from a Perl script to another
by GrandFather (Saint) on Oct 04, 2010 at 20:36 UTC

    If you have control over the second script make it what "Effective Perl Programming (second edition)" calls a modulino - a module that can be run as a script or used as a module. Consider:

    use strict; use warnings; package Demo; sub run { print "Hello world\n"; } run (@ARGV) if ! caller (); 1;

    which executes the run sub when run from the command line, or can be used in another script where you can call Demo::run () to have the same effect as running it from the command line, but with the advantage that it is now trivial to pass and return data.

    True laziness is hard work
Re: Passing arguments from a Perl script to another
by toolic (Bishop) on Oct 04, 2010 at 18:49 UTC
    To pass all args to your called script, try this:
    system ("$otherScript @ARGV test");
      But keep in mind that this way of calling also involves a shell which may interfere with possible shell-special characters in @ARGV.

      Probably a safer way would be this:

      system($^X, $otherscript, @ARGV, "test");
      which bypasses a shell ($^X is the path to the current Perl-interpreter).