note
broomduster
<blockquote><i>
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 ...
</i></blockquote>
It still works if you handle it properly:
<code>
#!/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'} }";
</code>
<p>
Firstly, don't dereference the array elements one at a time. Among other things, you get
<blockquote><i>
Use of uninitialized value in concatenation (.) or string at ./pm-886582 line 20.
</i></blockquote>
when there is no <c>-b</c> flag and args. But more important, you've hidden the fact that you really collected all of the arguments from all of the <c>-a</c> flags.
<p>
Now you will see that:
<c>
./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
</c>
(note that the output has an empty line where non-existent <c>@{ $options{'b'} }</c> is not printed.)
<p>IOW, all of the <c>-a</c> pairs are gathered into your <c>@{ $options{'a'} }</c>. 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.
<blockquote><i>
Thanks for the assist.
</i></blockquote>
My pleasure. One last word of caution. The docs mention that this is an experimental feature. <i>I</i> haven't had any problems with it so far, but YMMV.
886569
886582