develooper Front page | perl.perl5.porters | Postings from June 2003

RFC: Getopt::Long: options with multiple values

Thread Next
From:
Johan Vromans
Date:
June 25, 2003 01:02
Subject:
RFC: Getopt::Long: options with multiple values
Message ID:
16121.22256.93854.387328@phoenix.squirrel.nl
I'm interested in your opinions about the following.

In Getopt::Long, you can specify an array ref as recipient for the
option values, and multiple values will then be pushed in the array:

   my @foo;
   @ARGV = qw(--foo=abc --foo=def);
   GetOptions("foo=s" => \@foo);
   # Now $foo[0] will be "abc" and $foo[1] will be "def".

You can also put an "@" in the option specification:

   GetOptions("foo=s@");
   # Now $opt_foo[0] will be "abc" and $opt_foo[1] will be "def".

What should the following code do:

   my $foo;
   @ARGV = qw(--foo=abc --foo=def);
   GetOptions("foo=s@" => \$foo);

The current behaviour is quite counter-intuitive: it stores the last
of the values in the scalar. I think it is better to silently put an
array reference in the scalar, and store the values there:

   GetOptions("foo=s@" => \$foo);
   # Now $foo->[0] will be "abc" and $foo->[1] will be "def".

Basically, this will make the above code almost indentical to:

   $foo = [];
   GetOptions("foo=s@" => $foo);
   # Now $foo->[0] will be "abc" and $foo->[1] will be "def".

(Almost, in reality $foo will not be touched when no --foo option is
actually used.)

Alternatively, GetOptions could issue a (fatal) error that an array
reference is expected.

What do you think?

-- Johan

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About