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.
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:
$ 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 (but 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!