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
Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world
jfruh
Apple syncing patent can't come soon enough
pasmith
New Twitter features borrow from 3rd party clients
Esther Schindler
Open Source Changes the Software Acquisition Process
mikelgan
How to set up continuous podcast play on the new iTunes
David Strom
Five important Windows 7 mobility features
sjvn
Guard your Wi-Fi for your own sake
Sandra Henry-Stocker
Grepping on Whole Words
Sidekick: The Good News & the Bad News
Either way you look at it Microsoft Data Center management did not follow standards or best practices in this failure. In which case it makes me wonder more about the outsourcing of corporate data much less personal data.
- mburton325
Join the conversation here
Quick, practical advice for IT pros. Made fresh daily.
Want to cash in on your IT savvy? Send your tip to tips@itworld.com. If we post it, we'll send you a $25 Amazon e-gift card.












