I wanted to have the convenience of comma separated multivalued options while using Getopt::Long. The idiom for that is given in the pod:

my @libfiles; GetOptions ("library=s" => \@libfiles); @libfiles = split(/,/,join(',',@libfiles));
Trouble was, I had a lot of those options. Nonetheless, I just bulled ahead with:
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
That's repetitive. I started looking for a less redundant way to get that done.

My first thought was of a subroutine.
=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
Still redundant, and more lines of code. There is also the addition of c_split to the namespace.

The next idea was to have c_split act on the argument, passed by reference.

=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
That eliminates the assignments, but all the other objections remain.

Next idea was to pass all the arrays by reference and act on them in place.

=begin comment sub c_split { @$_ = split /,/, join ',', @$_ for @_; } c_split(\@foos, \@bars, \@bazen, \@quuxi); =end comment
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.

An anonymous sub? You bet! Here's what I ended up with:

(sub { @$_ = split /,/, join ',', @$_ for @_ }) -> (\@foos, \@bars, \@bazen, \@quuxi);
Zero redundancy and no namespace pollution.
I wonder if I went too far with that? ;-)

(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

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.