Perl
  Home arrow Perl arrow Page 8 - Processing Command Line Options with P...
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
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: 4 stars4 stars4 stars4 stars4 stars / 48
    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:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb 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

    Dell PowerEdge Servers

    Processing Command Line Options with PERL - Negative Reinforcement
    (Page 8 of 9 )

    An interesting feature in Getopt::Long.pm is its ability to also support option negation - that is, disable an option by prefixing it with "no". For example, this means that while you could explicitly activate the "trace" option by sending the program "--trace" on the command line, you could also explicitly disable it by sending "--no-trace". However, it's only possible to do this with Boolean options, which can be set to either true or false; you can't do it with options that require numeric or string values.

    In order to do this, simply add an exclamation (!) as negation symbol after the option name in the call to GetOptions() - as in the following example:


    #!/usr/bin/perl
     
    # import module
    use Getopt::Long;
     
    # read options
    $result = GetOptions ( "eyes!" => $sight,
        "nose!" => $smell,
        "ears!" => $hearing );  
          

    # print values
    $sight ? print "Can see!n" : print "Cannot see!n";
    $smell ? print "Can smell!n" : print "Cannot smell!n"; $hearing ?
    print "Can hear!n" : print "Cannot hear!n"; 

    A user could now do something like this:

    $ ./script.pl --no-eyes --no-ears --no-nose
    Cannot see!
    Cannot smell!
    Cannot hear!

    Note that there's an important difference between explicitly disabling an option in this manner, and not passing the option to the program at all. In the former case, Getopt::Long.pm sets the corresponding option variable to false or 0; in the latter, the option variable is null (or whatever default value it was assigned initially). Consider the following example, which illustrates this difference:


    #!/usr/bin/perl
     
    # import module
    use Getopt::Long;
     
    # default value for counter
    $counter = -1;
     
    # read options
    $result = GetOptions ("counter!" => $counter);  
          

    # print values
    print "Counter is $counter";

    Here's the output:

    $ ./script.pl --counter
    Counter is 1
    $ ./script.pl --no-counter
    Counter is 0
    $ ./script.pl
    Counter is -1

    More Perl Articles
    More By icarus, (c) Melonfire


       · Good article, very helpful, but there's a syntax error (for me, anyway) in the...
     

       

    PERL ARTICLES

    - Perl: A Continuing Look at Hashes and Multid...
    - Perl: Another Round with Hashes
    - Perl Hashes
    - Perl Lists: A Final Look at List::Util
    - Perl Lists: Utilizing List::Util
    - Perl Lists: The Split() Function
    - SQL and CGI with Perl and DBI
    - Perl Lists: More Functions and Operators
    - SELECT Queries and Perl
    - Perl Lists: More on Manipulation
    - Creating a Database with Perl and DBI
    - Perl: Sailing the List(less) Seas
    - Perl and DBI
    - Perl: Concatenating Text and More
    - Perl Text: Quoting Without Quote Marks

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway