Special Perl Variables

May 24, 2001, 12:00 AM —  ITworld — 

Last week, we discussed the input record separator variable ($/), one
of Perl's special global variables. Perl has a large number of special
variables (all listed and explained in the perlvar manpage), but really
you only need to be familiar with a handful (or two) for most
programming requirements. The following subset lists the most common
such variables, the remainder can be looked up in perlvar as needed.

$_ The default variable (often used, seldom seen).
$/ Input record separator (default is "\n").
$\ Output record separator (default is "").
$, Output field separator (default is "").
$" Field separator for interpolated arrays (default
is " ").
$| Autoflush variable for currently selected file handle.
$ARGV Name of current file being read by .
$. Current line number being read.
$0 The name of the current script.
$$ The current process id.
$1..$N Captured data in regular expressions.

Be aware that $ARGV provides the name of the current file being read
via , and remember that whenever you do something like this:

while (<>) {
print;
}

...the empty <> is either reading from STDIN, or ARGV (the latter if
there were any arguments in the @ARGV array). When reading from ARGV,
the $ARGV variable will be set to each filename in turn. Also, when
reading from ARGV, the $. variable does not automatically reset between
files, so it will represent the current total line number (see the
documentation for the eof() function to work around this).

You need to know some special array and hash variables:

@ARGV The command line argument array.
@_ The subroutine argument array.
%ENV Hash of environment variables.
%INC Hash of filenames that have been included (via do(),
require(), or use).
@INC Search paths to find included files.
@EXPORT List of things to export from a module by default.
@EXPORT_OK List of things to export from a module on demand.
@ISA Inheritance.

The latter variables listed above are only relevant for creating
modules; you shouldn't need them for general programming. The @INC
array lets Perl know where to look for modules or libraries that you
include via 'use', do(), or require(). By default, it holds all the
necessary paths created when Perl itself was built and installed (which
is where most new modules will be installed as well). Sometimes, you
need to install modules in non-standard places. You will need to insert
these paths into the @INC array so Perl can find them.

You can modify the @INC array in a couple of ways. The first option
uses the PERL5LIB environment variable. If that variable is defined
when you start your script, then the paths defined will be prepended to
the @INC array. You can use the 'use lib' pragma to add paths to this
array within your script. For example:

#!/usr/bin/perl -w
use strict;
use lib qw(/home/jandrew/perl/lib);
use MyModule;
...

The above, first prepends the path '/home/jandrew/perl/lib' to the @INC
array, then the call to 'use MyModule' will search that path first
trying to locate the module in question.

Next Week: Simple Object Oriented Tutorial: soot Part 1

» posted by ITworld staff

ITworld

I like it!
Post a comment
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.
Resources
White Paper

Symantec Backup Exec 12 and Backup Exec System Recovery 8 deliver industry leading Windows data protection and system recovery. Download this whitepaper to find out the top reasons to upgrade and how to get continuous data protection and complete system recovery.

Webcast

Data and system loss — from a hard drive failure, malicious attack, natural disaster, or simple human error — can happen anytime. Don’t leave your business vulnerable. Make sure you have a secure recovery strategy in place. Symantec's latest backup and system recovery technology can efficiently restore critical applications, individual emails and documents and even restore your entire system in minutes in the event of a loss.

White Paper

Businesses face a growing challenge to ensure that the IT environment is properly protected. Backup Exec 12 integrates with other applications in the Symantec family of products, to complement your current data protection strategy, keep your data securely backed up and make it recoverable when you need it most.

Free stuff

Enterprise 2.0 Implementation
By Aaron C. Newman, Jeremy Thomas
Published by McGraw-Hill
Learn more!

Deploying Cisco Wide Area Application Services
By Zach Seils, Joel Christner
Published by Cisco Press
Learn more!

Featured Sponsor

AISO founders envisioned a Web hosting company that was environmentally friendly. While the company employed energy-efficient innovations like solar panels, its infrastructure produced unacceptable power and cooling requirements. Find out how AISO leveraged AMD technology to overcome their challenge in this case study white paper.

In this whitepaper, Scalar explores the opportunity to change the landscape with respect to mission critical databases built around Oracle. Leveraging technologies such as Linux, high-end commodity processing power and Oracle RAC technology to architect, design, build and maintain database infrastructure that delivers maximum availability, reliability and performance at a fraction of traditional cost.

On a typical day, weather.com, the Web site for The Weather Channel in Atlanta, serves up between 15 million and 20 million page views. But in September 2004, when back-to-back hurricanes ransacked Florida, the peak traffic on one day more than tripled: over 70 million page views by more than 7 million unique visitors. Read the full success story now.

More Resources