I find myself surprised that Getopt::Long apaprently prefers the last case-insensitive match of an option name. Specifically:
#!/usr/bin/env perl use strict; use warnings; use Getopt::Long; #use Getopt::Long qw(:config no_ignore_case); my $opt_verbose = 0; my $opt_version = 0; GetOptions( "verbose|v" => \$opt_verbose, "version|V" => \$opt_version, ) or die "getopt failed"; print "verbose=$opt_verbose, version=$opt_version\n";
If I save this as try.pl and run try.pl -v, the result is verbose=0, version=1.

I would have expected either for the exact case match to be preferred, or getopt to complain, per its documentation:

With "ignore_case", option specifications for options that only differ in case, e.g., "foo" and "Foo", will be flagged as duplicates.
but apparently this does not happen for alternate names.

Workarounds are possible, such as the commented-out :config no_ignore_case, or switching the order of the option specifications. But it feels wrong that the result depends on the order given.

I don't expect the behavior to change after all these years, but maybe there is something to say in the doc. Or am I missing something already there that explains the behavior? It seems like this (v vs. V) would be such a common case that I doubt I'm discovering anything new here.

I tried with perl 5.36.1, also 5.24.0 (just an old version I had at hand), and (as expected) results were the same.

Thanks for any wisdom ... --karl


In reply to Getopt::Long case matching last wins by karlberry

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.