the only problem I have with this is defining parameters for %options before building %options with GetOptions If for instance I needed to have multiple pairs per flag ...
It still works if you handle it properly:
#!/usr/bin/perl use v5.10.1; use strict; use warnings; use Getopt::Long; my (@a, @b); my %options = ( 'a' => \@a, 'b' => \@b, ); GetOptions ( 'a=s{2}' => \@a, 'b=s{2}' => \@b, ); # say "$options{'a'}->[0], $options{'a'}->[1]"; # say "$options{'b'}->[0], $options{'b'}->[1]"; say "@{ $options{'a'} }"; say "@{ $options{'b'} }";

Firstly, don't dereference the array elements one at a time. Among other things, you get

Use of uninitialized value in concatenation (.) or string at ./pm-886582 line 20.
when there is no -b flag and args. But more important, you've hidden the fact that you really collected all of the arguments from all of the -a flags.

Now you will see that:

./pm-886582 -a a1_1 a1_2 -a a2_1 a2_2 -a a3_1 a3_2 a1_1 a1_2 a2_1 a2_2 a3_1 a3_2
(note that the output has an empty line where non-existent @{ $options{'b'} } is not printed.)

IOW, all of the -a pairs are gathered into your @{ $options{'a'} }. All you need now is some code to check that the array has an even number of elements and then to take it apart into (multiple) pairs.

Thanks for the assist.
My pleasure. One last word of caution. The docs mention that this is an experimental feature. I haven't had any problems with it so far, but YMMV.

In reply to Re^3: GetOpts::long Multiple Parameters per option by broomduster
in thread 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.