Hello all,
I am working on taking command line switches.
I would like to have an option take two parameters on the command line. i.e.:

./program.pl -a param1 param2 -b param3 param4

Initially I wrote the script to take one parameter per switch.

#!/usr/bin/perl use strict; use warnings; use Getopt::Long; my %options = (); GetOptions ( "a=s" => \$options{'a'}, "b=s" => \$options{'b'}, "z!" => \$options{'z'}, ); my %Subs = ( 'a' => \&do_a, 'b' => \&do_b, 'z' => \&do_z, ); while (my ($sub_name, $function) = each %Subs){ $function->($options{$sub_name}); } sub do_a { my $active = shift; return unless $active; my $param1 = $active; # my $param2 = shift @ARGV; # print "$param1, $param2, \n"; } sub do_b { my $active = shift; return unless $active; my $param1 = $active; # my $param2 = shift @ARGV; # print "$param1, $param2, \n"; } sub do_z { my $active = shift; return unless $active; #do stuff without parameters }

To take two parameters I added the line:

my $param2 = shift @ARGV; #commented out in the code above

this works great if the parameters are passed in order:

$perl program.pl -a param1_a param2_a -b param1_b param2_b param1_a, param2_a, param1_b, param2_b,

but if I switch the order that I call the options the in the reverse order the second parameters are passed to the wrong sub:

$perl program.pl -b param1_b param2_b -a param1_a param2_a param1_a, param2_b, param1_b, param2_a,

Can anyone suggest a method for taking two parameters from the command line?


In reply to GetOpts::long Multiple Parameters per option by PyrexKidd

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.