Perl
  Home arrow Perl arrow 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
    ( Page 1 of 4 )

    Unlike command line arguments, command line options can sometimes be a bit difficult to read; nevertheless, they can prove to be quite useful. This article explains what a command line option is, why you would use one, and how to process them in Perl. This article is the first of two parts.

    Command line arguments play a significant role in the creation of command line programs in any language. These arguments can tell the program a number of things, such as what to operate on, or how to operate on something. Perl, like most other languages, makes it very easy to read these arguments.

    However, sometimes it's necessary to interact with a command line program differently. Sometimes, it's necessary to change a program's behavior in order to turn off or on functionality, or to adjust some setting in some way. 

    For example, consider the program cat. Using it, it's possible to print the contents of a given file to standard output:

     

    $ cat file.txt

     

    It's also possible to number the lines that are outputted. This is done with a command line option:

     

    $ cat -n file.txt

     

    Command line options, also known as flags or switches, make it possible to pass optional instructions to a command line program. Unlike command line arguments, however, these are a bit harder to read, especially since they can take a number of forms. 

    In this article, we'll take a look at a few ways to process command line options in Perl. 

    A short introduction 

    Before we get started with Perl code, let's take a minute to conduct a very basic overview of command line options. First, command line options can either be short or long. Short command line options are represented by a hyphen followed by a single letter (that's why they're “short”). Consider the example earlier:

     

    $ cat -n file.txt

     

    Above, we use a short command line option. Long options, on the other hand, can contains multiple letters and are usually preceded by two hyphens (although some applications use only one). Sometimes, an application allows both forms to be used. In fact, cat allows this. We could rewrite the last example like this, to use a long option:

     

    $ cat --number file.txt

     

    Command line options can serve in an on/off role, or they can have values after them. The number option above is an example of on/off behavior. In that case, we're turning numbering on. All that's needed for this behavior is the name of the option. 

    If an option has a value after it, the placement of that value may differ depending on whether the option is short or long. For a short option, the value may be placed with a space between the option name and the value:

     

    $ ls -T 1

     

    Or, if the value is a number, then it may come immediately after the option name:

     

    $ ls -T1

     

    For a long option, the value is placed after an equals sign:

     

    $ ls --tabsize=1

     

    Also, if there are multiple short options with no values associated, they may be spaced out:

     

    $ cat -n -v file.txt

     

    Or, more commonly, they can be combined, with no spaces in between:

     

    $ cat -nv file.txt

     

    This is known as “bundling.” The key thing to remember, though, is that bundled options do not have values. 



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