Special Perl Variables

May 23, 2001, 11:00 PM —  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

Sign up for ITworld's Daily newsletter
Follow ITworld on Twitter @IT_world

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.
peer-to-peer

Esther Schindler
If the comments are ugly, the code is ugly

claird
SVG a graphics format for 21st century

pasmith
Take Chrome OS for a test spin

Sandra Henry-Stocker
Solaris Tip: Have Your Files Changed Since Installation?

sjvn
64-bits of protection?

jfruh
Android fragments vs. the iPhone monolith

mikelgan
What Gizmodo missed about the Pro WX Wireless USB disk drive

 

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

The Daily Tip

The Daily TipQuick, practical advice for IT pros. Made fresh daily.

Hot tips:

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.

Newsletters

Subscribe to ITWORLD TODAY and receive the latest IT news and analysis.

I would like to receive offers via email from ITworld partners.
By clicking submit you agree to the terms and conditions outlined in ITworld's privacy policy.
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.

Marketplace