jordanh has asked for the wisdom of the Perl Monks concerning the following question:
In the docs it gives all the examples in the form:
Where the option variables are initialized to '' (or some other default).my $tag = ''; # option variable with default value GetOptions ('tag=s' => \$tag);
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?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Getopt::Long to define or not to define?
by moot (Chaplain) on Mar 04, 2005 at 20:26 UTC | |
by jordanh (Chaplain) on Mar 04, 2005 at 21:52 UTC | |
|
Re: Getopt::Long to define or not to define?
by shemp (Deacon) on Mar 05, 2005 at 00:07 UTC | |
by fokat (Deacon) on Mar 05, 2005 at 15:49 UTC | |
by jordanh (Chaplain) on Mar 05, 2005 at 16:08 UTC | |
by shemp (Deacon) on Mar 07, 2005 at 23:27 UTC |