Perl
  Home arrow Perl arrow Page 9 - Processing Command Line Options with 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? 
PERL

Processing Command Line Options with PERL
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 52
    2004-03-31


    Table of Contents:
  • Processing Command Line Options with PERL
  • The POSIX Standard
  • Down To Work
  • GetOptions() Function
  • Half-Life
  • Getopt::Long.pm
  • Opting In
  • Negative Reinforcement
  • Hashing It Up

  • 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


    Processing Command Line Options with PERL - Hashing It Up
    ( Page 9 of 9 )

    If you'd prefer, you can also store all command-line options directly in a hash, instead of creating separate scalars for each. The procedure is fairly simple; all you need to do is pass a reference to the hash variable as the first argument to GetOptions(), followed by the names of the options to be stored in it. Consider the following example:


    #!/usr/bin/perl
     
    # import module
    use Getopt::Long;
     
    # read options into hash
    $result = GetOptions (%options, "base=i", "height=i");  
          

    # print hash values
    print "Base = " . $options{'base'} . "n";
    print "Height = " . $options{'height'} . "n";
    print "Area = " .  0.5 * $options{'base'} * $options{'height'} . "n";

    Here's the output:

    $ ./triangle.pl --base=10 --height=20
    Base = 10
    Height = 20
    Area = 100

    In this case, the values passed to the program on the command line are stored in a Perl hash called %options, from whence they can be retrieved using standard hash notation.

    Over And Out

    And that's about all we have time for. Over the course of the last few pages, I introduced you to one of the more interesting modules in the Perl pantheon, the Getopt::Long.pm module. This module provides a simple API to parse options passed to your Perl scripts at the command line and convert them into Perl scalars or arrays.

    With some simple examples, I showed you how to use this API to detect Boolean options, as well as options taking string or numeric values. I also showed you how to create option aliases, and explicitly allow disabling of options with the negation symbol. Finally, I wrapped things up with a demonstration of how you could convert all the options passed on the command-line into a Perl hash, and also showed you how to customize the module to work as per your specific requirements.

    If you'd like to read more about this module, consider visiting the following links:

    CPAN, at http://www.cpan.org/

    Getopt::Long pages on CPAN, at
    http://search.cpan.org /search?module=Getopt::Long

    Till next time...stay healthy!

    Note: Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. 



     
     
    >>> More Perl Articles          >>> More By icarus, (c) Melonfire
     

       

    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 6 Hosted by Hostway
    Stay green...Green IT