Logo         Community
  Trog
Services
The Company
Community
Columns
Your Account
Contact Us
 
 
Understanding Perl's Special Variables
Get familiar with Perl's special built-ins

| In Default |

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:

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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

''.preg_replace(array('/  /', '/ /'), array('  ', '   '), '
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.


How to do Everything with PHP & MySQL
How to do Everything with PHP & MySQL, the best-selling book by Melonfire, explains how to take full advantage of PHP's built-in support for MySQL and link the results of database queries to Web pages. You'll get full details on PHP programming and MySQL database development, and then you'll learn to use these two cutting-edge technologies together. Easy-to-follow sample applications include a PHP online shopping cart, a MySQL order tracking system, and a PHP/MySQL news publishing system.

Read more, or grab your copy now!


previous page more like this  print this article  next page
 
Search...
 
In trog...
Logging With PHP
Building A Quick-And-Dirty PHP/MySQL Publishing System
Output Buffering With PHP
Date/Time Processing With PHP
Creating Web Calendars With The PEAR Calendar Class
more...
 
In the hitg report...
Crime Scenes
Animal Attraction
Lord Of The Strings
more...
 
In boombox...
Patience - George Michael
Think Tank - Blur
My Private Nation - Train
more...
 
In colophon...
Hostage - Robert Crais
The Dead Heart - Douglas Kennedy
Right As Rain - George Pelecanos
more...
 
In cut!...
American Chai
The Core
Possession
more...
 
Find out how you can use this article on your own Web site!


Copyright © 1998-2018 Melonfire. All rights reserved
Terms and Conditions | Feedback