Perl Programming Page 8 - Processing Command Line Options with PERL |
An interesting feature in Getopt::Long.pm is its ability to also support option negation - that is, disable an option by prefixing it with "no". For example, this means that while you could explicitly activate the "trace" option by sending the program "--trace" on the command line, you could also explicitly disable it by sending "--no-trace". However, it's only possible to do this with Boolean options, which can be set to either true or false; you can't do it with options that require numeric or string values. In order to do this, simply add an exclamation (!) as negation symbol after the option name in the call to GetOptions() - as in the following example:
A user could now do something like this:
Note that there's an important difference between explicitly disabling an option in this manner, and not passing the option to the program at all. In the former case, Getopt::Long.pm sets the corresponding option variable to false or 0; in the latter, the option variable is null (or whatever default value it was assigned initially). Consider the following example, which illustrates this difference:
Here's the output:
blog comments powered by Disqus |