Getopt::Long is a basic part of the Perl toolset. However one minor nit that Ive had up to now is that specifying the arguments and their destinations, along with defaults seemed a bit clumsy under strict. Take an example from the documentation

use GetOpt::Long; my $verbose = ''; # option variable with default value (false) my $all = ''; # option variable with default value (false) GetOptions ('verbose' => \$verbose, 'all' => \$all);

To me this is inelegant. I have two "declarations" about a variable in separate locations, one the real declaration along with the default value, and the other the association of the option name (which may be different from the variable name) to the variable. If I want to rename a variable for clarity later on I have to change two items. Now perhaps this is blindingly obvious to the rest of you but I just realized that you can instead write this

use GetOpt::Long; #Adjust bractes and commas to taste GetOptions ( 'verbose' => \(my $verbose = ''), 'all' => \(my $all = ''), );

Which to me seems preferable. Everything is in one place. It says "this variable normally comes from outside", it puts the declaration of the variable next to the declaration of the option handling it is associated with, and it provides a way to document the whole lot in one place if you need to.

Any thoughts?


In reply to GetOpt::Long usage style by demerphq

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, 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, 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.