Hi all.

I just noticed something I can't understand with Getopt::Long yesterday. From the manual:

Options with multiple values
Options sometimes take several values. For example, a program could use multiple directories to search for library files:
    --library lib/stdlib --library lib/extlib
To accomplish this behaviour, simply specify an array reference as the destination for the option:
    GetOptions ("library=s" => \@libfiles);
Alternatively, you can specify that the option can have multiple values by adding a "@", and pass a scalar reference as the destination:
    GetOptions ("library=s@" => \$libfiles);

And this is what I have

use strict; use warnings; use Getopt::Long; use Data::Dumper; ##### THREE VARIATIONS ##### ## Does work: my $options_ok = GetOptions ('option=s@' => \(our $option_array_ref)); my @option_array = @$option_array_ref; ## Does work: our $option_array; my $options_ok = GetOptions ('option=s' => \@option_array); ## Doesn't work: my $options_ok = GetOptions ('option=s' => \(our @option_array)); ##### COMMON CODE ##### if (not $options_ok) { print "Problem with options\n"; exit -1; } print Dumper(@option_array); exit 0;

Run with something like:

perl script.pl --option OPTION1 --option OPTION2
The strange thing is that this works for a single argument option:
my $options_ok = GetOptions ('option=s' => \(our $option_variable));

So I had a bunch of those (saves lines for declaration of the variables), and when I wanted to switch one into a multi-value version, it didn't behave as I expected…

Wonder if anybody has an explanation, it must have to do with how references to variables and strings are returned on declaration, but it's not clear in my mind.
Any insights appreciated.

Cheers, Mark Collins.


In reply to Issue with multiple options for a Getopt::Long argument by Anonymous Monk

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.