A Perl getopts example

Perl getopts FAQ: Can you demonstrate how to use the getopts function? (Also written as, "Can you demonstrate how to read Perl command line arguments?")

In an earlier tutorial I wrote about How to read Perl command line arguments, which demonstrates how to read simple command line arguments (like a filename) in a Perl script. But in that article I didn't discuss the use of command-line flags (also called options or arguments) in a Perl program. In this article I'll demonstrate how to read command-line options in Perl with the Getopt package.

Perl getopts - handling command line options/flags

As a quick introduction, a couple of years ago I wrote a Unix command named Teleport, which is an improvement on the Unix cd command. That script takes several command line options, including the -h flag, which lists help information:

tp -h

and the -l command provides a list of directories you've visited previously:

tp -l

All told, there are six command-line options (flags) that this command accepts.

In this tutorial I'll demonstrate how to handle these command line options (flags) in a Perl program.

A Perl getopts example - Handling command-line options

Perl follows the Unix and Linux tradition of handling command-line options by using a "getopts" package. Once you've include the Getopt package in your environment, this example shows how to use a series of five flags in a Perl program:

#!/usr/bin/perl -w

# a perl getopts example
# alvin alexander, http://www.devdaily.com

use strict;
use Getopt::Std;

# declare the perl command line flags/options we want to allow
my %options=();
getopts("hj:ln:s:", \%options);

# test for the existence of the options on the command line.
# in a normal program you'd do more than just print these.
print "-h $options{h}\n" if defined $options{h};
print "-j $options{j}\n" if defined $options{j};
print "-l $options{l}\n" if defined $options{l};
print "-n $options{n}\n" if defined $options{n};
print "-s $options{s}\n" if defined $options{s};

# other things found on the command line
print "Other things found on the command line:\n" if $ARGV[0];
foreach (@ARGV)
  print "$_\n";

Here are some notes about this Perl getopts example:

  • This script lets the user specify five possible options with the letters h, j, l, n, and s.
  • The Perl options j, n, and s expect something to come after the flag, which you can tell by the ":" character in the getopts string shown above.
  • To use the Perl getopts functionality, you need to use the Getopt::Std package, as shown above.
  • All this program does is print the options that are used. In a real-world program you'd do something more important after checking each flag. (I show more of a real-world example below.)
  • The foreach loop at the end of the program prints everything else that may be on the command line. I'll discuss this next.

Using this Perl getopts script

This Perl script lets you use command line flags like this:

tp -h


tp -n 5


tp -s foo

You can also combine several command line options, like this:

tp -h -l -s foo -n 5

Besides that, you don't have to specify any command-line options, and can run the command like this:

tp foo


tp foo bar baz

A more real-world Perl getopts example

The Perl script shown above would be slightly more "real world" if I did something besides print whether a command line flag was set or not. For instance, in a real world program you'll add some checks and functions that look like this:

if ($options{h})

sub do_help {
  print "Would have shown help.";

In a real-world program you'll want to have checks like these because it often doesn't make sense for a user to use multiple command line options at the same time. For instance, in my actual program, it doesn't make sense to combine the -h, -j, -l, -n, or -s flags, they are all mutually exclusive.

Perl getops - Summary

I hope these Perl getopts examples have been helpful. Again, you use this Perl getopts functionality whenever you want to process Perl command line options, or flags. Also, to make your Unix/Linux programming life easier for you, the Perl getopts functionality is similar to many other Unix-based languages.