Perl
  Home arrow Perl arrow 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? 
Google.com  
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
    ( Page 1 of 9 )

    Over the course of the next few pages, I will introduce 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.

    If you've ever used a shell (UNIX or MS-DOS) before, you're probably already familiar with command-line options - they're the little arguments you pass to a program you're executing on the command line in order to modify its behaviour. For example, if you're deleting a directory, you could add a parameter to the command line to tell the system to delete all sub-directories under it as well or, if you're retrieving a directory listing with the "ls" command, you could add the "-l" command-line option to obtain a detailed listing instead of the abbreviated version.

    Now, I'm not going to wax eloquent on the wonders of command-line options - the concept is a trivial one, and not so hard to understand that it needs more than a couple of lines of explanation. Rather, I'm going to get into something infinitely more interesting - showing you, the developer, how to add support for command-line options to your Perl program.

    It's not as difficult as it might seem at first glance - and no, you don't need to stay up all night to get it done. All you really need is a copy of the Getopt::Long.pm Perl module, and a little imagination. Come on in, and be prepared to be amazed...this is potent stuff!

    The Long And Short Of It

    First, we need to get the definitions down. In case you're wondering what a module is, don't; for the purpose of this article, just assume that it's a thingamajig that allows you to add new capabilities to your Perl program, or a series of pre-rolled functions which can be plugged in to your Perl program.

    There are a number of such modules out there. CPAN, the Comprehensive Perl Archive Network, has a complete list. Most are available free of charge, and are simple to import into your Perl program. If you're running a fairly recent version of Perl, you probably already have Getopt::Long.pm installed as part of your distribution; if not, drop by CPAN and get yourself a copy.

    You're probably wondering just what Getopt::Long.pm brings to the Perl party. Let me enlighten you.

    The Getopt::Long.pm module provides a way for developers to read options passed to their program on the command line and act on them. Typically, such options provide a way for users to control the behavior of the program on an as-needed basis, by passing optional arguments to it; these options are usually preceded by a dash, as in the following example:

    $ ls -l

    In this case, "-l" is an optional argument passed to the "ls" program on
    the command line.



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