I would like to pass a named argument with 3 (or more) values using Getopt::Long. To complicate things a bit, the 3 arguments include strings and operators (for an eval call) and I would like to use the argument multiple times. Here is an approach I've considered with a string with comma delimiter:

perl evaluateit.pl --list 'strA,==,100' --list 'strB,>,1000' #!/usr/bin/env perl use strict; use warnings; use Getopt::Long; my %opts = (); my $getoptsresult = GetOptions(\%opts,"list=s@"); my %h = (strA=>100,strB=>999); foreach(@{$opts{list}}){ my @args = split /\,/; my $code = join(' ',$h{$args[0]},$args[1],$args[2]); my $answer = eval($code); print "answer: $answer\n"; #do something with answer }
The complicating factor seems to be that I need to include the --list option many times so defining individual options for the 3 elements isn't practical. There is an example in the Perl 5 v16.2 documentation that looks promising (--coordinates 52.2 16.4 --rgbcolor 255 255 149) but there is a warning that it is experimental and there is no hint as to how to implement it.

Any comments about passing multiple named arguments each with lists of values would be helpful.


In reply to getopt long, named argument with multiple values by jmf11

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.