Perl
  Home arrow Perl arrow Page 4 - 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 - Responding to command line arguments
    ( Page 4 of 4 )

     

    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 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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek