Home arrow Perl Programming arrow Processing Command Line Options with PERL

Processing Command Line Options with 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



If you've ever used a shell (UNIX or MS-DOS) before, you're probably already familiar with command-line options - they're the little arguments you pass to a program you're executing on the command line in order to modify its behaviour. For example, if you're deleting a directory, you could add a parameter to the command line to tell the system to delete all sub-directories under it as well or, if you're retrieving a directory listing with the "ls" command, you could add the "-l" command-line option to obtain a detailed listing instead of the abbreviated version.

Now, I'm not going to wax eloquent on the wonders of command-line options - the concept is a trivial one, and not so hard to understand that it needs more than a couple of lines of explanation. Rather, I'm going to get into something infinitely more interesting - showing you, the developer, how to add support for command-line options to your Perl program.

It's not as difficult as it might seem at first glance - and no, you don't need to stay up all night to get it done. All you really need is a copy of the Getopt::Long.pm Perl module, and a little imagination. Come on in, and be prepared to be amazed...this is potent stuff!

The Long And Short Of It

First, we need to get the definitions down. In case you're wondering what a module is, don't; for the purpose of this article, just assume that it's a thingamajig that allows you to add new capabilities to your Perl program, or a series of pre-rolled functions which can be plugged in to your Perl program.

There are a number of such modules out there. CPAN, the Comprehensive Perl Archive Network, has a complete list. Most are available free of charge, and are simple to import into your Perl program. If you're running a fairly recent version of Perl, you probably already have Getopt::Long.pm installed as part of your distribution; if not, drop by CPAN and get yourself a copy.

You're probably wondering just what Getopt::Long.pm brings to the Perl party. Let me enlighten you.

The Getopt::Long.pm module provides a way for developers to read options passed to their program on the command line and act on them. Typically, such options provide a way for users to control the behavior of the program on an as-needed basis, by passing optional arguments to it; these options are usually preceded by a dash, as in the following example:

$ ls -l

In this case, "-l" is an optional argument passed to the "ls" program on
the command line.

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