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

Responding to command line arguments - 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.

TABLE OF CONTENTS:
  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
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
 

The GetOptions function, as we've used it now, sets variables according to the values of command line options, or whether or not command line options are present. We can respond to these command line options by examining the variables later on the program. Sometimes, this is the appropriate approach. 

Other times, however, this is really inconvenient. Suppose we need more control over what happens when a command line option is encountered. Perhaps we want to set a variable whose value is determined by modifying the value from the command line. 

In order to do this, we can use a subroutine reference as a command line option's destination in the GetOptions function. This subroutine will be called, with the first argument passed being the option name, and the second option being the value of that option (normally, but we won't get into the exception in this article). 

Say that we want to condense multiple spaces in an option's value. We could examine the variable set by GetOptions later on, but why not just collapse everything into one step? We can create a subroutine that does this for us, and then we can reference the subroutine in GetOptions. 

Here's a script that does just that:

 

#!/usr/bin/perluse strict;use Getopt::Long;my $longstring = '';GetOptions('longstring=s' => &process_longstring);print "longstring: $longstringn";sub process_longstring { $longstring = @_[1]; $longstring =~ s/ {2,}/ /;}

 

It's also possible to pass an anonymous subroutine. This works best for situations where we're not doing a lot of work. Here's our last script's GetOptions function call, rewritten to use an anonymous subroutine:

 

GetOptions('longstring=s' => sub { $longstring = @_[1]; $longstring =~ s/ {2,}/ /; } );

 

We'll end our exploration of the Getopt::Long module here. Although there are a number of additional features, I'll leave them to you to uncover. You should at least have the basics down now, which are enough to create functional programs that take advantage of command line options.



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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PERL PROGRAMMING ARTICLES

- 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: