Command Line Options in Perl: Using Getopt::Std

Unlike command line arguments, command line options can sometimes be a bit difficult to read; nevertheless, they can prove to be quite useful. This article explains what a command line option is, why you would use one, and how to process them in Perl. This article is the first of two parts.

Command line arguments play a significant role in the creation of command line programs in any language. These arguments can tell the program a number of things, such as what to operate on, or how to operate on something. Perl, like most other languages, makes it very easy to read these arguments.

However, sometimes it’s necessary to interact with a command line program differently. Sometimes, it’s necessary to change a program’s behavior in order to turn off or on functionality, or to adjust some setting in some way. 

For example, consider the program cat. Using it, it’s possible to print the contents of a given file to standard output:


$ cat file.txt


It’s also possible to number the lines that are outputted. This is done with a command line option:


$ cat -n file.txt


Command line options, also known as flags or switches, make it possible to pass optional instructions to a command line program. Unlike command line arguments, however, these are a bit harder to read, especially since they can take a number of forms. 

In this article, we’ll take a look at a few ways to process command line options in Perl. 

A short introduction 

Before we get started with Perl code, let’s take a minute to conduct a very basic overview of command line options. First, command line options can either be short or long. Short command line options are represented by a hyphen followed by a single letter (that’s why they’re “short”). Consider the example earlier:


$ cat -n file.txt


Above, we use a short command line option. Long options, on the other hand, can contains multiple letters and are usually preceded by two hyphens (although some applications use only one). Sometimes, an application allows both forms to be used. In fact, cat allows this. We could rewrite the last example like this, to use a long option:


$ cat –number file.txt


Command line options can serve in an on/off role, or they can have values after them. The number option above is an example of on/off behavior. In that case, we’re turning numbering on. All that’s needed for this behavior is the name of the option. 

If an option has a value after it, the placement of that value may differ depending on whether the option is short or long. For a short option, the value may be placed with a space between the option name and the value:


$ ls -T 1


Or, if the value is a number, then it may come immediately after the option name:


$ ls -T1


For a long option, the value is placed after an equals sign:


$ ls –tabsize=1


Also, if there are multiple short options with no values associated, they may be spaced out:


$ cat -n -v file.txt


Or, more commonly, they can be combined, with no spaces in between:


$ cat -nv file.txt


This is known as “bundling.” The key thing to remember, though, is that bundled options do not have values. 

{mospagebreak title=Parsing short options with Getopt::Std} 

Let’s start by taking a look at the Getopt::Std module. This module provides two functions for parsing short options and isn’t very difficult to get started with. With each method, you simply need to specify the letters for short options, and then the module stores the values of those options in the appropriate variables. 

For example, consider a Perl script named optapp. This application can take two short command line options, a and b. Both of these options take values. We might call this script like this:


$ ./optapp -a20 -b30


In order to actually create this script, we’d use the getopt function of the Getopt::Std module. This function takes one argument, a string containing the letters of all command line options to be recognized. Also, with the getopt function, these command line options must take values. 

This function is called near the top of the script, and after being called, it will set global variables named $opt_x, where x is the letter of the option. The values of the variables will be the values associated with the options. In the case of the above call, $opt_a would be set to 20, and $opt_b would be set to 30. 

The resulting script would look like this:


#!/usr/bin/perluse Getopt::Std;getopt('ab');print "$opt_an" if (defined $opt_a);print "$opt_bn" if (defined $opt_b);


As you can see, the script simply prints out the values of the options, if they are used at all. Run the program, passing in some values with the options, and see what you get. 

Notice, though, that the script doesn’t use the strict pragma. If we do use it, then Perl will complain, since $opt_a and $opt_b were never declared. This is a problem, but it’s a problem that’s easily fixed. We simply need to put in an "our" declaration for each of the names we plan to use. 

Note that we can’t just use "my." We must use "our." 

Now we can use the strict pragma:


#!/usr/bin/perluse strict;use warnings;use Getopt::Std;our($opt_a, $opt_b);getopt('ab');print "$opt_an" if (defined $opt_a);print "$opt_bn" if (defined $opt_b);


Still, using global variables to store option values is a bit ugly, and every time you need to define a new command line option, it’s necessary to declare the associated name using our. Fortunately, we’re able to use a hash instead, which makes things easier and keeps our commands neat. 

To store option values in a hash, just pass a hash reference to the getopt function as the second argument. The keys of the hash correspond to the option names. Here’s part of our script rewritten to use a hash rather than global variables:


my %opt;getopt('ab', %opt);print $opt{'a'} . "n" if (defined $opt{'a'});print $opt{'b'} . "n" if (defined %opt{'b'});


The same thing can be accomplished with either method. Just use whatever method you feel looks better. 

{mospagebreak title=Parsing options without values} 

The getopt function works fine if you’re only dealing with options that take values. However, you’ll often need to parse options that do not take values, or some mix of the two. The getopt function does not work for this. Fear not, though, because the getopts function does the trick. 

The getopts function operates similarly to the getopt function. It takes a minimum of one argument, which is a string of single-letter options that the program recognizes. However, in this string, you can specify which options take values and which options don’t. If a letter appears by itself in the string, then it doesn’t take a value, but if there is a colon after the letter, then it does take a value. 

The getopts function, like the getopt function, can also take a hash reference in which to store the option values. Otherwise, it will use variables. 

To see this in action, let’s recreate the script from the last section. However, this time, let’s add a third option, c, which does not take a value. Here’s the new script (minus the beginning bit):


my %opt;getopts('a:b:c', %opt);print $opt{'a'} . "n" if (defined $opt{'a'});print $opt{'b'} . "n" if (defined $opt{'b'});print "c option in usen" if ($opt{'c'});


As you can see, the program will take two options, a and b, each of which takes a value since a colon follows the letters. Note how we choose to store the values in a hash. The values of this hash corresponding to options a and b will be, of course, the option values. This is the same as before. For c, however, the value will either be 0 or 1. If the option is used, the value is 1. Otherwise, the value is 0. 

{mospagebreak title=Special options} 

The Getopt::Std module provides one extra bit of functionality that’s worth mentioning. Often, you’ll want to provide switches that will make the program display help information or print version information. You can define and check for these switches yourself, printing out the relevant information if necessary. This isn’t very difficult. However, it’s also possible to let Getopt::Std do the checking for you. 

For help information, Getopt::Std will check for “–help.” Note that this is a long option. If this switch is present, then the module will print out basic help information, such as version information and a list of options. The only thing you should do is set $Getopt::Std::STANDARD_HELP_VERSION to a true value:




If we use the help switch on our last example, the output will look something like this:


./getopt_6 version [unknown] calling Getopt::Std::getopts (version 1.05),

running under Perl version 5.10.0.


Usage: getopt_6 [-OPTIONS [-MORE_OPTIONS]] [–] [PROGRAM_ARG1 …]


The following single-character options are accepted:

With arguments: -a -b

Boolean (without arguments): -c


Options may be merged together. — stops processing of options.

Space is not required between options and their arguments.


For version information, Getopt::Std will check for “–version.” The output will, by default, look something like this:


./getopt_6 version [unknown] calling Getopt::Std::getopts (version 1.05),

running under Perl version 5.10.0.


Notice how this same information is printed first in the help output. 

It’s possible to change these messages, though, by simply defining two functions, HELP_MESSAGE and VERSION_MESSAGE. Both functions are passed a file handle, and all that’s necessary is for you to write the proper message to that file handle. Let’s define both methods to provide some more appropriate messages:


sub HELP_MESSAGE { my $fh = shift; print $fh "t-attTakes a valuen"; print $fh "t-bttTakes a value as welln"; print $fh "t-cttDoes not take a valuen"; print $fh "nt--helpttDisplays thisn"; print $fh "t--versiontDisplays version informationn";}sub VERSION_MESSAGE { my $fh = shift; print $fh "My Application version 1.0n";}


Now, when the help or version switches are used, the program will print out the new, custom messages automatically. That saves you, the developer, a bit of time and effort in checking for the switches yourself.

[gp-comments width="770" linklove="off" ]

chat sex hikayeleri Ensest hikaye