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

Getopt::Long.pm - 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



It is interesting to note that the equality symbol (=) sign between the option name and the data type specifier tells Getopt::Long.pm that an option value *must* be provided for the option. Look what happens if, for example, you specify the "--age" option without a corresponding integer value:

$ ./script.pl --age
Option age requires an argument

In order to make an option value optional (try saying that fast!), use a colon (:) instead of an equality symbol (=), as below:

# import module
use Getopt::Long;
# read options
$result = GetOptions ("age:i" => $age);  

# print value
print "Input age is $age years";

Here, in the absence of a value for the option, Getopt::Long.pm will automatically assign 0 (for integer values) or an empty string (for string values) to the corresponding option variable. Take a look at the output of the script above to verify this:

$ ./script.pl --age
Input age is 0 years

>>> 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: