Logo         Community
The Company
Your Account
Contact Us
Processing Command-Line Options With Perl
Add support for command-line options to your Perl program.

| 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, at http://www.cpan.org/ , has a complete list - and 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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
$ ls -l

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

Under the POSIX standard, it's also possible to use longer, more readable command-line options, preceded with a double dash, as in the following example:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
$ ls --color

The Getopt::Long.pm module provides an API for Perl developers to capture these long command-line options, and act on them within the business logic of the Perl script. This API is pretty advanced - it ignores case differences in option names, can resolve abbreviated option names to their longer counterparts (so long as they are unique), and recognizes both single- and double-dashes as option prefixes. For purists and those who are tasked with porting legacy applications, the module also supports the older, single-character form of command-line options (although only if they belong to the alphabetic set).

There is one primary function in the Getopt::Long.pm module - GetOptions() - and it serves as the main control point for you to access the options passed to the program. You can use this to read command-line options into Perl scalars, arrays or hashes; create user-defined subroutines to handle specific options; separate option "bundles" into individual units; and configure the behaviour of the module. More on some of these as we proceed through this tutorial.

Getopt::Long.pm is written in the best traditions of object-oriented programming, fondly known as OOP. If you're a fan of OOP, you can create a Getopt::Long object, which has its own methods and properties, and use standard OO syntax to access its functions, extend it, sub-class it, derive new hybrids from it...all kinds of good stuff, basically. In case you don't know what OOP is, you're probably not impressed. Good for you!

How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!

previous page more like this  print this article  next page
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
In cut!...
American Chai
The Core
Find out how you can use this article on your own Web site!

Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback