PERLVAR(1) Perl Programmers Reference Guide PERLVAR(1)
NAME
perlvar - Perl predefined variables
DESCRIPTION
The Syntax of Variable Names
Variable names in Perl can have several formats. Usually, they must begin with a letter or underscore, in which case they can be
arbitrarily long (up to an internal limit of 251 characters) and may contain letters, digits, underscores, or the special sequence "::" or
"'". In this case, the part before the last "::" or "'" is taken to be a package qualifier; see perlmod.
Perl variable names may also be a sequence of digits or a single punctuation or control character. These names are all reserved for special
uses by Perl; for example, the all-digits names are used to hold data captured by backreferences after a regular expression match. Perl has
a special syntax for the single-control-character names: It understands "^X" (caret "X") to mean the control-"X" character. For example,
the notation $^W (dollar-sign caret "W") is the scalar variable whose name is the single character control-"W". This is better than typing
a literal control-"W" into your program.
Since Perl 5.6, Perl variable names may be alphanumeric strings that begin with control characters (or better yet, a caret). These
variables must be written in the form "${^Foo}"; the braces are not optional. "${^Foo}" denotes the scalar variable whose name is a
control-"F" followed by two "o"'s. These variables are reserved for future special uses by Perl, except for the ones that begin with "^_"
(control-underscore or caret-underscore). No control-character name that begins with "^_" will acquire a special meaning in any future
version of Perl; such names may therefore be used safely in programs. $^_ itself, however, is reserved.
Perl identifiers that begin with digits, control characters, or punctuation characters are exempt from the effects of the "package"
declaration and are always forced to be in package "main"; they are also exempt from "strict 'vars'" errors. A few other names are also
exempt in these ways:
ENV STDIN
INC STDOUT
ARGV STDERR
ARGVOUT
SIG
In particular, the special "${^_XYZ}" variables are always taken to be in package "main", regardless of any "package" declarations
presently in scope.
SPECIAL VARIABLES
The following names have special meaning to Perl. Most punctuation names have reasonable mnemonics, or analogs in the shells.
Nevertheless, if you wish to use long variable names, you need only say:
use English;
at the top of your program. This aliases all the short names to the long names in the current package. Some even have medium names,
generally borrowed from awk. To avoid a performance hit, if you don't need the $PREMATCH, $MATCH, or $POSTMATCH it's best to use the
"English" module without them:
use English '-no_match_vars';
Before you continue, note the sort order for variables. In general, we first list the variables in case-insensitive, almost-lexigraphical
order (ignoring the "{" or "^" preceding words, as in "${^UNICODE}" or $^T), although $_ and @_ move up to the top of the pile. For
variables with the same identifier, we list it in order of scalar, array, hash, and bareword.
General Variables
$ARG
$_ The default input and pattern-searching space. The following pairs are equivalent:
while (<>) {...} # equivalent only in while!
while (defined($_ = <>)) {...}
/^Subject:/
$_ =~ /^Subject:/
tr/a-z/A-Z/
$_ =~ tr/a-z/A-Z/
chomp
chomp($_)
Here are the places where Perl will assume $_ even if you don't use it:
o The following functions use $_ as a default argument:
abs, alarm, chomp, chop, chr, chroot, cos, defined, eval, exp, glob, hex, int, lc, lcfirst, length, log, lstat, mkdir, oct, ord,
pos, print, quotemeta, readlink, readpipe, ref, require, reverse (in scalar context only), rmdir, sin, split (on its second
argument), sqrt, stat, study, uc, ucfirst, unlink, unpack.
o All file tests ("-f", "-d") except for "-t", which defaults to STDIN. See "-X" in perlfunc
o The pattern matching operations "m//", "s///" and "tr///" (aka "y///") when used without an "=~" operator.
o The default iterator variable in a "foreach" loop if no other variable is supplied.
o The implicit iterator variable in the "grep()" and "map()" functions.
o The implicit variable of "given()".
o The default place to put an input record when a "<FH>" operation's result is tested by itself as the sole criterion of a "while"
test. Outside a "while" test, this will not happen.
As $_ is a global variable, this may lead in some cases to unwanted side-effects. As of perl 5.9.1, you can now use a lexical
version of $_ by declaring it in a file or in a block with "my". Moreover, declaring "our $_" restores the global $_ in the current
scope.
Mnemonic: underline is understood in certain operations.
@ARG
@_ Within a subroutine the array @_ contains the parameters passed to that subroutine. Inside a subroutine, @_ is the default array
for the array operators "push", "pop", "shift", and "unshift".
See perlsub.
$LIST_SEPARATOR
$" When an array or an array slice is interpolated into a double-quoted string or a similar context such as "/.../", its elements are
separated by this value. Default is a space. For example, this:
print "The array is: @array
";
is equivalent to this:
print "The array is: " . join($", @array) . "
";
Mnemonic: works in double-quoted context.
$PROCESS_ID
$PID
$$ The process number of the Perl running this script. You should consider this variable read-only, although it will be altered across
"fork()" calls.
Note for Linux users: on Linux, the C functions "getpid()" and "getppid()" return different values from different threads. In order
to be portable, this behavior is not reflected by $$, whose value remains consistent across threads. If you want to call the
underlying "getpid()", you may use the CPAN module "Linux::Pid".
Mnemonic: same as shells.
$REAL_GROUP_ID
$GID
$( The real gid of this process. If you are on a machine that supports membership in multiple groups simultaneously, gives a space
separated list of groups you are in. The first number is the one returned by "getgid()", and the subsequent ones by "getgroups()",
one of which may be the same as the first number.
However, a value assigned to $( must be a single number used to set the real gid. So the value given by $( should not be assigned
back to $( without being forced numeric, such as by adding zero. Note that this is different to the effective gid ($)) which does
take a list.
You can change both the real gid and the effective gid at the same time by using "POSIX::setgid()". Changes to $( require a check
to $! to detect any possible errors after an attempted change.
Mnemonic: parentheses are used to group things. The real gid is the group you left, if you're running setgid.
$EFFECTIVE_GROUP_ID
$EGID
$) The effective gid of this process. If you are on a machine that supports membership in multiple groups simultaneously, gives a
space separated list of groups you are in. The first number is the one returned by "getegid()", and the subsequent ones by
"getgroups()", one of which may be the same as the first number.
Similarly, a value assigned to $) must also be a space-separated list of numbers. The first number sets the effective gid, and the
rest (if any) are passed to "setgroups()". To get the effect of an empty list for "setgroups()", just repeat the new effective gid;
that is, to force an effective gid of 5 and an effectively empty "setgroups()" list, say " $) = "5 5" ".
You can change both the effective gid and the real gid at the same time by using "POSIX::setgid()" (use only a single numeric
argument). Changes to $) require a check to $! to detect any possible errors after an attempted change.
$<, $>, $( and $) can be set only on machines that support the corresponding set[re][ug]id() routine. $( and $) can be swapped only
on machines supporting "setregid()".
Mnemonic: parentheses are used to group things. The effective gid is the group that's right for you, if you're running setgid.
$PROGRAM_NAME
$0 Contains the name of the program being executed.
On some (but not all) operating systems assigning to $0 modifies the argument area that the "ps" program sees. On some platforms
you may have to use special "ps" options or a different "ps" to see the changes. Modifying the $0 is more useful as a way of
indicating the current program state than it is for hiding the program you're running.
Note that there are platform-specific limitations on the maximum length of $0. In the most extreme case it may be limited to the
space occupied by the original $0.
In some platforms there may be arbitrary amount of padding, for example space characters, after the modified name as shown by "ps".
In some platforms this padding may extend all the way to the original length of the argument area, no matter what you do (this is
the case for example with Linux 2.2).
Note for BSD users: setting $0 does not completely remove "perl" from the ps(1) output. For example, setting $0 to "foobar" may
result in "perl: foobar (perl)" (whether both the "perl: " prefix and the " (perl)" suffix are shown depends on your exact BSD
variant and version). This is an operating system feature, Perl cannot help it.
In multithreaded scripts Perl coordinates the threads so that any thread may modify its copy of the $0 and the change becomes
visible to ps(1) (assuming the operating system plays along). Note that the view of $0 the other threads have will not change since
they have their own copies of it.
If the program has been given to perl via the switches "-e" or "-E", $0 will contain the string "-e".
On Linux as of perl 5.14 the legacy process name will be set with prctl(2), in addition to altering the POSIX name via "argv[0]" as
perl has done since version 4.000. Now system utilities that read the legacy process name such as ps, top and killall will
recognize the name you set when assigning to $0. The string you supply will be cut off at 16 bytes, this is a limitation imposed by
Linux.
Mnemonic: same as sh and ksh.
$SUBSCRIPT_SEPARATOR
$SUBSEP
$; The subscript separator for multidimensional array emulation. If you refer to a hash element as
$foo{$a,$b,$c}
it really means
$foo{join($;, $a, $b, $c)}
But don't put
@foo{$a,$b,$c} # a slice--note the @
which means
($foo{$a},$foo{$b},$foo{$c})
Default is "