Greetings Knowledgeable Monks,

I recently shot myself in the foot with Getopt::Long by setting up a hash of default parameters, and then misspelling one of the parameter names in the GetOptions call, like this:

# set up default values for parameters: my %pars = ( item => 'default value', ); GetOptions ( \%pars, 'items=s', # NOTE MISPELLING-- "items" s/b "item" ) or die "bad GetOptions $!"; # use $pars{item}, which is unaffected by an --items argument
That is, my typo autovivifies a new entry in the parameter hash, which is then never used. It took a painful amount of time to figure out why the arguments weren't responding correctly!

Does anyone have a clever way of checking for mistakes (typos) of this type? The best I can do is something like this:

my @opts = ( { name => 'item1', type => '=s', dval => 'default_value1' }, { name => 'item2', type => '=s', dval => 'default_value2' }, ); my %pars = map { $opts[$_]{name} , $opts[$_]{dval} } 0..$#opts; my @args = map { $opts[$_]{name} . $opts[$_]{type} } 0..$#opts; GetOptions ( \%pars, @args ) or die "bad GetOptions $!";
but I'd love to see something else that could protect me from my typos, if someone has something better.

In reply to Getopt::Long Gotcha, request advice by kesterkester

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.