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.
Getopt::Long.pm can also process command-line arguments containing multiple values, simply by storing all the values in a Perl array. Consider, for example, the following script, which is designed to add email addresses to a subscription list (maybe for an email newsletter?). Here, the user can send as many email addresses as (s)he likes to the script, simply by repeating the "--add" option with different values. Take a look:
Getopt::Long.pm also supports aliases for options, allowing you to provide users with an alternative, sometimes shorter way of accessing the same option. This is accomplished by placing alternative option names after the first one and separating the various alternatives with pipes (|). Consider the following example, which shows you how:
# print value $color ? print "Colors are on" : print "Colors are off";
Here, you can attach any of the options "--color", "--colour", "--c" or "-c" to the command line - Getopt::Long.pm will treat them all as one and the same.
$ ./script.pl --color Colors are on $ ./script.pl --c Colors are on $ ./script.pl -c Colors are on $ ./script.pl --colour Colors are on