I use Getopt::Long.

In the docs it gives all the examples in the form:

my $tag = ''; # option variable with default value GetOptions ('tag=s' => \$tag);
Where the option variables are initialized to '' (or some other default).

I typically declare the option variables, but don't give them an initial value, depending on the fact that they are undefined to flag to me that the option is unspecified (as opposed to set to a false value).

I think this mode of operation is supported, but I'm wondering if there isn't a pitfall or practice that I'm not thinking of here. Actually, I think there's a clear implication that this mode is supported in the docs where it says:

The call to GetOptions() parses the command line arguments that are present in @ARGV and sets the option variable to the value 1 if the option did occur on the command line. Otherwise, the option variable is not touched. Setting the option value to true is often called enabling the option.

My way of working with it seems natural to me, but the examples and other people's code often seems to follow the other style.

I recognized the pitfall that an undefined value tests false and that I need to explicitly test for defined.

Is there anything else I'm missing?


In reply to Getopt::Long to define or not to define? by jordanh

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.