Home arrow Perl Programming arrow Page 8 - Processing Command Line Options with PERL

Negative Reinforcement - Perl

Over the course of the next few pages, I will introduce you to one of the more interesting modules in the Perl pantheon, the Getopt::Long.pm module. This module provides a simple API to parse options passed to your Perl scripts at the command line and convert them into Perl scalars or arrays.

  1. Processing Command Line Options with PERL
  2. The POSIX Standard
  3. Down To Work
  4. GetOptions() Function
  5. Half-Life
  6. Getopt::Long.pm
  7. Opting In
  8. Negative Reinforcement
  9. Hashing It Up
By: icarus, (c) Melonfire
Rating: starstarstarstarstar / 58
March 31, 2004

print this article



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:

# import module
use Getopt::Long;
# read options
$result = GetOptions ( "eyes!" => $sight,
    "nose!" => $smell,
    "ears!" => $hearing );  

# print values
$sight ? print "Can see!n" : print "Cannot see!n";
$smell ? print "Can smell!n" : print "Cannot smell!n"; $hearing ?
print "Can hear!n" : print "Cannot hear!n"; 

A user could now do something like this:

$ ./script.pl --no-eyes --no-ears --no-nose
Cannot see!
Cannot smell!
Cannot hear!

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:

# import module
use Getopt::Long;
# default value for counter
$counter = -1;
# read options
$result = GetOptions ("counter!" => $counter);  

# print values
print "Counter is $counter";

Here's the output:

$ ./script.pl --counter
Counter is 1
$ ./script.pl --no-counter
Counter is 0
$ ./script.pl
Counter is -1

>>> More Perl Programming Articles          >>> More By icarus, (c) Melonfire

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: