I wanted to have the convenience of comma separated multivalued options while using Getopt::Long. The idiom for that is given in the pod:
Trouble was, I had a lot of those options. Nonetheless, I just bulled ahead with:my @libfiles; GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(',',@libfiles));
That's repetitive. I started looking for a less redundant way to get that done.use Getopt::Long; my (@foos, @bars, @bazen, @quuxi); GetOptions ( 'foos=s' => \@foos, 'bars=s' => \@bars, 'bazen=s' => \@bazen, 'quuxi=s' => \@quuxi ); # Obnoxious first cut podded out =begin comment @foos = split /,/, join ',', @foos; @bars = split /,/, join ',', @bars; @bazen = split /,/, join ',', @bazen; @quuxi = split /,/, join ',', @quuxi; =end comment
Still redundant, and more lines of code. There is also the addition of c_split to the namespace.=begin comment sub c_split { split /,/, join ',', @_; } @foos = c_split(@foos); @bars = c_split(@bars); @bazen = c_split(@bazen); @quuxi = c_split(@quuxi); =end comment
The next idea was to have c_split act on the argument, passed by reference.
That eliminates the assignments, but all the other objections remain.=begin comment sub c_split (\@) { my $aref = shift; @$aref = split /,/, join ',', @$aref; } c_split(@foos); c_split(@bars); c_split(@bazen); c_split(@quuxi); =end comment
Next idea was to pass all the arrays by reference and act on them in place.
Much better, but now there is a named sub defined which is only called once. It's too specialized to be of much general use.=begin comment sub c_split { @$_ = split /,/, join ',', @$_ for @_; } c_split(\@foos, \@bars, \@bazen, \@quuxi); =end comment
An anonymous sub? You bet! Here's what I ended up with:
Zero redundancy and no namespace pollution.(sub { @$_ = split /,/, join ',', @$_ for @_ }) -> (\@foos, \@bars, \@bazen, \@quuxi);
(Added) ++calin, I knew I was getting obsessive, but I didn't realize I had the blinders on!
After Compline,
Zaxo
In reply to Getting Sparse - Taming Multivalued Options in Getopt::Long by Zaxo
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |