Perl
  Home arrow Perl arrow Using Getopt::Long: More Command Line Options in Perl
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid  
Request Media Kit
Contact Us  
Site Map  
Privacy Policy  
Support  
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
Google.com  
PERL

Using Getopt::Long: More Command Line Options in Perl
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 2
    2009-05-19


    Table of Contents:
  • Using Getopt::Long: More Command Line Options in Perl
  • Handling values
  • Optional values and multiple values
  • Responding to command line arguments

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article

     
     
    ADVERTISEMENT


    Using Getopt::Long: More Command Line Options in Perl
    ( Page 1 of 4 )

    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.

    The Getopt::Std module allows you to read and respond to short options, that can either take values or function as boolean values. The module even aids in the creation of help and version information, taking much of the work out of things.

    However, sometimes you'll need to support a large number of command line options, and representing each of them with a single letter becomes quite impractical after a certain point. Besides, short options can easily become very cryptic for the end user of your application. 

    Fortunately, Perl makes it very easy to work with long options too, which, as you remember, can be represented by multiple letters. By making use of the appropriately-named Getopt::Long module, you can easily add broader support for command line options into your application. 

    In this article, we'll take a look at the Getopt::Long module and the functionality it offers for processing command line options. 

    Getting started 

    With Getopt::Std, we only had to call one function to process all of the command line options. That one function would then populate global variables (by default) with option values. Getopt::Long also has a single function, GetOptions, which handles the command line options, but it's more complex. This function takes an even number of arguments, where the odd-numbered arguments are the option names (actually, they are the “specifications” and can contain more than just the names, but we'll get into that later), and the even-numbered arguments are references to variables where the option values will be stored (again, it gets more complex than this—these are actually the “destinations”). 

    Let's see what this looks like in an actual script. Consider a program that can take just one command line option. This command line option will be named “force” and will make the program force some sort of operation. It doesn't matter what that operation is because we're not concerned with that here. We're only concerned with the command line options, so let's just print out the value of the command line option. Obviously, the option will have a boolean value, since it doesn't take any values. If it's present, it's true. Otherwise, it's false. 

    We could call the program like this:

     

    $ program --force

     

    Or like this:

     

    $ program

     

    Here's what the full script looks like:

     

    #!/usr/bin/perluse strict;use Getopt::Long;my $force = '';GetOptions('force' =>  $force);print "force: $forcen";

     

    As you can see, there's nothing complicated here. The only thing you should keep in mind is that the variables you use need default values. Otherwise, if the command line option isn't present, and you use the variable later on, you'll get a warning if you have warnings enabled. 



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

       

    PERL ARTICLES

    - More Perl Bits
    - Perl, Bit by Bit
    - Basic Charting with Perl
    - Using Getopt::Long: More Command Line Option...
    - Command Line Options in Perl: Using Getopt::...
    - Web Access with LWP
    - More Templating Tools for Perl
    - Site Layout with Perl Templating Tools
    - Build a Perl RSS Aggregator with Templating ...
    - Looping, Security, and Templating Tools
    - Perl: Bon Voyage Lists and Hashes
    - Templating Tools
    - Perl: Number Crunching
    - Perl Debuggers in Detail
    - Debugging Perl





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek