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

Understanding Perl's Special Variables
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 30
    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:
      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

    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

    - 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 3 hosted by Hostway