Perl
  Home arrow Perl arrow Page 3 - Command Line Options in Perl: Using Getopt::Std
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

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


    Table of Contents:
  • Command Line Options in Perl: Using Getopt::Std
  • Parsing short options with Getopt::Std
  • Parsing options without values
  • Special options

  • 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


    Command Line Options in Perl: Using Getopt::Std - Parsing options without values
    ( Page 3 of 4 )

     

    The getopt function works fine if you're only dealing with options that take values. However, you'll often need to parse options that do not take values, or some mix of the two. The getopt function does not work for this. Fear not, though, because the getopts function does the trick. 

    The getopts function operates similarly to the getopt function. It takes a minimum of one argument, which is a string of single-letter options that the program recognizes. However, in this string, you can specify which options take values and which options don't. If a letter appears by itself in the string, then it doesn't take a value, but if there is a colon after the letter, then it does take a value. 

    The getopts function, like the getopt function, can also take a hash reference in which to store the option values. Otherwise, it will use variables. 

    To see this in action, let's recreate the script from the last section. However, this time, let's add a third option, c, which does not take a value. Here's the new script (minus the beginning bit):

     

    my %opt;getopts('a:b:c', %opt);print $opt{'a'} . "n" if (defined $opt{'a'});print $opt{'b'} . "n" if (defined $opt{'b'});print "c option in usen" if ($opt{'c'});

     

    As you can see, the program will take two options, a and b, each of which takes a value since a colon follows the letters. Note how we choose to store the values in a hash. The values of this hash corresponding to options a and b will be, of course, the option values. This is the same as before. For c, however, the value will either be 0 or 1. If the option is used, the value is 1. Otherwise, the value is 0. 



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