in reply to Getting Sparse - Taming Multivalued Options in Getopt::Long

why use two passes? Give GetOptions a sub to call instead.

use Getopt::Long; my (@foos, @bars, @bazen, @quuxi); GetOptions ( 'foos=s' => sub { shift; push @foos, split /,/, @_; }, 'bars=s' => sub { shift; push @bars, split /,/, @_; }, 'bazen=s' => sub { shift; push @bazen, split /,/, @_; }, 'quuxi=s' => sub { shift; push @quuxi, split /,/, @_; }, );

of course, since your option names exactly match your arrays, something like this should work...

use Getopt::Long; my (@foos, @bars, @bazen, @quuxi); sub setter { no strict refs; push @{shift}, split /,/, @_; } GetOptions ( 'foos=s' => \setter, 'bars=s' => \setter, 'bazen=s' => \setter, 'quuxi=s' => \setter, );

but at that point you should just use...

use Getopt::Long; my %opts; sub setter { push @{$opts{shift}}, split /,/, @_; } GetOptions ( 'foos=s' => \setter, 'bars=s' => \setter, 'bazen=s' => \setter, 'quuxi=s' => \setter, );

(All of that is completely untested ... I'm sure I have some syntax typos).

Update: Yeah, What Roy said....

Replies are listed 'Best First'.
Re^2: Getting Sparse - Taming Multivalued Options in Getopt::Long
by Roy Johnson (Monsignor) on Jun 06, 2004 at 23:39 UTC
    ...which leads to
    GetOptions ( map {"$_=s" => \setter} qw(foos bars bazen quuxi) );
    right?

    The PerlMonk tr/// Advocate
Re: Getting Sparse - Taming Multivalued Options in Getopt::Long
by Abigail-II (Bishop) on Jun 07, 2004 at 08:58 UTC
    my (@foos, @bars, @bazen, @quuxi); sub setter { no strict refs; push @{shift}, split /,/, @_; }
    What's the point of the my? If you are using symbolic references, you will work with package variables, not lexical variables (well, that's assuming you use @{+shift}, otherwise you'll be pushing on @shift).

    Abigail