Perl
  Home arrow Perl arrow Page 2 - 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Understanding Perl's Special Variables - In Default
    (Page 2 of 11 )

    One of the more important special variables you'll encounter in your Perl forays is $_, which is used by many Perl functions and constructs as the so-called "default variable" when none other exists. In order to demonstrate, consider the following example:


    #!/usr/bin/perl

    # set array
    %stuff = ("phone", "Nokia", "car", "BMW", "gun", "Smith & Wesson");

    # iterate over array
    foreach $s (keys(%stuff))
    {
    print "$s\n";
    }

    Here's the output:


    car
    gun
    phone

    In this case, the foreach() loop iterates over the hash, assigning the key
    at each iteration to the $s instance variable; this variable is then
    printed with the print() function. If, however, you omit the instance
    variable in the foreach() loop, Perl will assume that you want it to use
    the default $_ variable as the instance variable.

    The following example, which is equivalent to the one above, demonstrates:


    #!/usr/bin/perl

    # set array
    %stuff = ("phone", "Nokia", "car", "BMW", "gun", "Smith & Wesson");

    # iterate over array
    foreach (keys(%stuff))
    {
    print "$_\n";
    }

    Here, every time the loop iterates over the hash, since no instance variable is specified, Perl will assign the key to the default variable $_. This variable can then be printed via a call to print().

    The $_ variable also serves as the default for both chop() and print() functions. Going back to the example above, you could also write it this way,


    #!/usr/bin/perl

    # set array
    %stuff = ("phone", "Nokia", "car", "BMW", "gun", "Smith & Wesson");

    # iterate over array
    foreach (keys(%stuff))
    {
    print;
    }

    which would return


    cargunphone

    The $_ variable is also the default variable used by file and input handles. For example, consider the following simple Perl script, which prints back whatever input you give it:


    #!/usr/bin/perl

    # read from standard input
    # print it back
    while (<STDIN>)
    {
    print $_;
    }

    In this case, every line read by the standard input is assigned to the $_ variable, which is then printed back out with print().

    Knowing what you now know about print() and $_, it's clear that you could also write the above as


    #!/usr/bin/perl

    # read from standard input
    # print it back
    while (<STDIN>)
    {
    print;
    }

    The $_ default variable is used in a number of different places: it is the default variable used for pattern-matching and substitution operations; the default variable used by functions like print(), chop(), grep(), ord(), etc; the default instance variable in foreach() loops; and the default used for various file tests.

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