Special Perl Variables
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
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
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.
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.
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.
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!








