Posted on 2006-01-18 00:16:50-08 by perlslicker2
pushing options into an array ref. possible?
I'd like to know if its possible to store options in both variables and hash lists. ie:

GetOptions (
'var=s' => \$var,
'list=s%' => \$list # hash list
);

so on the command line i would have these options:
--var some_string --list add=first --list add=second --list add=third

Where each successive 'list add' option will push the value of add into array
ref $list->{'add'}.

I want the resulting data objects to be:
$var = 'some_string'
$list->{'add'}= qw(first second third);

My current setup will clobber the first two instances of '--list add' and only recognize the last '--list add=third' because the value is a straight hash and not a hash list.

Direct Responses: 2609 | Write a response
Posted on 2006-07-11 07:55:39-07 by jvromans in response to 1633
Re: pushing options into an array ref. possible?
That's where user defined destination routines can be used for:
use Getopt::Long; use strict; my $var; my $list; GetOptions ('var=s' => \$var, 'list=s%' => sub { push(@{$list->{$_[1]}}, $_[2]) }, ); use Data::Dumper; print Dumper $list;
Direct Responses: Write a response
Perl Weekly newsletter
A free weekly newsletter for people who are busy to read all the blogs. click here to check it out.