Perl
  Home arrow Perl arrow Page 9 - 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 - Rank And File
    (Page 9 of 11 )

    When reading data from files, Perl allows you to obtain the name of the
    file with the $ARGV variable, and the line number with the $. variable.
    This makes it easy to obtain information on which file (and which line of
    the file) is under the gun at any given moment. Consider the following
    example, which demonstrates:


    #!/usr/bin/perl

    # read a file and print its contents
    while (<>)
    {
    # for each line, print file name, line number and contents
    print $ARGV, ",", $., ": ";
    print;
    }

    Here's an example of the output:


    multiply.pl,1: #!/usr/bin/perl
    multiply.pl,2:
    multiply.pl,3: # multiply two numbers
    multiply.pl,4: sub multiply()
    multiply.pl,5: {
    multiply.pl,6: my ($a, $b) = @_;
    multiply.pl,7: return $a * $b;
    multiply.pl,8: }
    multiply.pl,9:
    multiply.pl,10: # set range for multiplication table
    multiply.pl,11: @values = (1..10);
    multiply.pl,12:
    multiply.pl,13: # get number from command-line
    multiply.pl,14: foreach $value (@values)
    multiply.pl,15: {
    multiply.pl,16: print "$ARGV[0] x $value = " . &multiply($ARGV[0],
    $value) . "\n";
    multiply.pl,17: }

    Note, however, that the line number returned by $. does not automatically
    reset itself when used with multiple files, but rather keeps incrementing.
    In order to have the variable reset to 0 every time a new file is opened,
    you need to explicitly close() the previous file before opening a new one.

    The $0 variable returns the file name of the currently-running Perl script,
    as illustrated below:


    #!/usr/bin/perl

    # print filename
    print "My name is $0";

    Here's the output:


    My name is /tmp/temperature.pl

    The $$ variable returns the process ID of the currently-running Perl
    process, as below:


    #!/usr/bin/perl

    # print process ID
    print "This script is owned by process ID $$. Collect them all!";

    Here's the output:


    This script is owned by process ID 2209. Collect them all!

    Finally, the special variable $] always contains information on the Perl
    version you are currently running. So, for example, the program


    #!/usr/bin/perl

    # print Perl version
    print "Running Perl $]";

    might return something like this:


    Running Perl 5.008

    This, coupled with the $0 and $$ variables explained earlier, can come in
    handy when debugging misbehaving Perl programs,


    #!/usr/bin/perl

    # check for error
    if ($error == 1)
    {
    # write to error log with script name, PID and Perl version
    open (FILE, ">>/tmp/error.log");
    print FILE "Error in $0 (perl $] running as PID $$\n";
    close (FILE);
    }

    or even to perform version checks to ensure that your code only works with
    a specific Perl version.


    #!/usr/bin/perl

    # check version
    # display appropriate message
    if ($] < 5.0)
    {
    die("You need a more recent version of Perl to run this program");
    }
    else
    {
    print "This is Perl 5 or better";
    }

    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