Perl
  Home arrow Perl arrow Page 5 - Understanding Perl's Special Variables
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

Understanding Perl's Special Variables
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 34
    2003-07-10


    Table of Contents:
  • Understanding Perl's Special Variables
  • In Default
  • Input...
  • ...And Output
  • Getting Into An Argument
  • The Right Path
  • To Err Is Human
  • A Question Of Ownership
  • Rank And File
  • Calling For A Translator
  • End Zone

  • 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


    Understanding Perl's Special Variables - Getting Into An Argument
    ( Page 5 of 11 )

    Perl comes with some very interesting variables specifically designed to
    store input arguments, for both scripts and subroutines. The first of these
    is the special @ARGV array, which contains a list of all the command-line
    arguments passed to the Perl program; each argument is indexed as an
    element of the array. Consider the following example, which demonstrates:


    #!/usr/bin/perl

    # get length of argument list
    $num = @ARGV;

    # iterate and print arguments
    for ($x=0; $x<$num; $x++)
    {
    print "Argument " . ($x+1) . " is $ARGV[$x]\n";
    }

    Here's an example of the output (I called this script with the command-line
    arguments "red 5px Arial"):


    Argument 1 is red
    Argument 2 is 5px
    Argument 3 is Arial

    Perl also comes with a variable named @_, which contains arguments passed
    to a subroutine, and which is available to the subroutine when it is
    invoked. The value of each element of the array can be accessed using
    standard scalar notation - $_[0] for the first element, $_[1] for the
    second element, and so on.

    In order to illustrate, consider the following example:


    #!/usr/bin/perl

    # define a subroutine
    sub add_two_numbers
    {
    $sum = $_[0] + $_[1];
    return $sum;
    }

    $total = &add_two_numbers(3,5);
    print "The sum of the numbers is $total\n";

    In the example above, once the &add_two_numbers subroutine is invoked with
    the numbers 3 and 5, the numbers are transferred to the @_ variable, and
    are then accessed using standard scalar notation within the subroutine.
    Once the addition has been performed, the result is returned to the main
    program, and displayed on the screen via the print() statement.



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