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

hi, i call a perl script passing some parameters to the script one of which being a another script name which also needs parameters. but when i pass the parameters to script which is to be called from the main script, the main script shows me some warnings. ex: perl a.pl -a 1 -b b.pl -a 2 -b 2 -c 1 where the second a and b parameters are for the script b.pl and not a.pl, where as c is parameter to a.pl. Is there a way so that i can tell perl to take all this as single parameter. thanks for any help in advance

Replies are listed 'Best First'.
Re: pass command line parameters
by Joost (Canon) on Jun 08, 2006 at 10:31 UTC
Re: pass command line parameters
by wulvrine (Friar) on Jun 08, 2006 at 11:41 UTC
    If you want the entire argument string try using @ARGV

    #!/usr/bin/perl use strict; use warnings; my (@args) = @ARGV; print "args = @args\n";

    Call this script with some arguments:br> perl x.pl -a b -c d -g hask

    It would produce an output:
    args = -a b -c d -g hask

    At this point you would have to do the work of parsing through the array and grouping what you want where.
    Hope that helps!

    s&&VALKYRIE &&& print $_^q|!4 =+;' *|
Re: pass command line parameters
by albert (Monk) on Jun 08, 2006 at 10:38 UTC
    A different solution could be to incorporate 'b.pl' as a subroutine within 'a.pl'.

    -albert

Re: pass command line parameters
by rodion (Chaplain) on Jun 08, 2006 at 13:45 UTC
    From my reading of your question, I think the code below is what your looking for. With the right options, Getopt::Long parses the command line for you, up to the subsidiary perl script, and leaves the rest of the line intact for you to call call that script, or modify things.
    use strict; use Getopt::Long; my $fst_a_opt_value; my $fst_b_opt_value; print "ARGV before = @ARGV\n"; Getopt::Long::Configure("pass_through","require_order"); GetOptions( 'a=i' => \$fst_a_opt_value, 'b' => \$fst_b_opt_value, ); print "first a = $fst_a_opt_value \n"; print "first b = $fst_b_opt_value \n"; print "ARGV after = @ARGV\n"; # #prints # ARGV before = -a 1 -b b.pl -a 2 -b 2 -c 1 # first a = 1 # first b = 1 # ARGV after = b.pl -a 2 -b 2 -c 1
Re: pass command line parameters
by kabeldag (Hermit) on Jun 09, 2006 at 09:35 UTC
    Im not convinced that what you are doing is the correct way to your goal (which I am unsure exactly what that is).

    I would have code handle all arguments via initial command line, then branch out depending on what needed to be done. If you needed to call a new module or script, do it via code, not via the command line.

    Here is my crappy (super duper crappy wappy) possibility to your probable goal :
    use strict; my $a_a_switch_param; my $a_b_switch_param; my $c_b_switch_param; my $b_a_switch_param; my $b_b_switch_param; for (my $argcnt=0;$argcnt<=$#ARGV;$argcnt++) { my $arg=$ARGV[$argcnt]; if($arg eq "-a") { $argcnt++; $a_a_switch_param=$ARGV[$argcnt]; }elsif($arg eq "-b") { $argcnt++; $a_b_switch_param=$ARGV[$argcnt]; }elsif($arg eq "-c") { $argcnt++; $a_c_switch_param=$ARGV[$argcnt]; }elsif($arg eq "-ba") { $argcnt++; $b_a_switch_param=$ARGV[$argcnt]; }elsif($arg eq "-bb") { $argcnt++; $b_b_switch_param=$ARGV[$argcnt]; } } my $b_prog_output=qx/perl b.pl $b_a_switch_param $b_b_switch_param/;