Home arrow Perl Programming arrow Page 3 - Using Getopt::Long: More Command Line Options in Perl

Optional values and multiple values - Perl

In the last article, we took a look at command line options and how we could read them in Perl using the Getopt::Std module. For many simple applications, that article covers all that you need to make use of command line options. When you need more command line options, you can make use of the Getopt::Long module, which is the subject of this article.

  1. Using Getopt::Long: More Command Line Options in Perl
  2. Handling values
  3. Optional values and multiple values
  4. Responding to command line arguments
By: Peyton McCullough
Rating: starstarstarstarstar / 2
May 19, 2009

print this article



We just looked at how you can make a command line option take a value. However, there are a few more things you can do with values that are worth mentioning. 

First, you can make a value optional for a given command line option. For example, consider a program that normally operates silently, producing no command line output. However, you can give the user the option of making the program produce some output. Many programs operate like this. You can make a command line option called “verbose” that, when present, makes the program produce some output:


$ program --verbose


You might want to give the user the option to make the program print out even more output, though. This can be done by making the command line argument take an integer value that tells the program how verbose to be. A neat solution, though, would be to make this value optional. If the user specifies a value, then that value is used. Otherwise, it defaults to zero. Here's a script that handles such an option:


#!/usr/bin/perluse strict;use Getopt::Long;my $verbose = 0;GetOptions('verbose:i' =>  $verbose);print "verbose: $verbosen";


Second, you'll sometimes want a command line option to be used multiple times with multiple values. The Getopt::Long module allows for this. You simply need to pass an array reference rather than a scalar reference for the destination. 

Consider a command line option called “name” that takes a name. We might want to pass multiple names into the program by using this command line option more than once. So, we might run the program like this:


$ program --name Bob --name Anne


Here's how this would look like in Perl:


my @names;GetOptions('names=s' =>  @names);print "$_n" for (@names);


The @names array would contain an element for each value. If we called the program as we did above, it would contain “Bob” and “Anne.” 

>>> More Perl Programming Articles          >>> More By Peyton McCullough

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- Perl Turns 25
- Lists and Arguments in Perl
- Variables and Arguments in Perl
- Understanding Scope and Packages in Perl
- Arguments and Return Values in Perl
- Invoking Perl Subroutines and Functions
- Subroutines and Functions in Perl
- Perl Basics: Writing and Debugging Programs
- Structure and Statements in Perl
- First Steps in Perl
- Completing Regular Expression Basics
- Modifiers, Boundaries, and Regular Expressio...
- Quantifiers and Other Regular Expression Bas...
- Parsing and Regular Expression Basics
- Hash Functions

Developer Shed Affiliates


Dev Shed Tutorial Topics: