Some variables have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($
, @
, or %
), such as $_
. The explicit, long-form names shown are the variables' equivalents when you use the English module by including "use English;
" at the top of your program.
4.4.1 Global Special Variables
The most commonly used special variable is $_
, which contains the default input and pattern-searching string. For example, in the following lines:
The first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed inforeach ('hickory','dickory','doc') { print; }
$_
, and is used by default by print
. Here are the places where Perl will assume $_
even if you don't specify it:
-
Various unary functions, including functions like
ord
andint
, as well as the all file tests (-f
,-d
) except for-t
, which defaults toSTDIN
. -
Various list functions like
print
andunlink
. -
The pattern-matching operations
m//
,s///
, andtr///
when used without an=~
operator. -
The default iterator variable in a
foreach
loop if no other variable is supplied. -
The implicit iterator variable in the
grep
andmap
functions. -
The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a
while
test (i.e., <filehandle
>). Note that outside of awhile
test, this will not happen.
The following is a complete listing of global special variables:
-
The default input and pattern-searching space.
-
The current input line number of the last filehandle that was read. An explicit close on the filehandle resets the line number.
-
The input record separator; newline by default. If set to the null string, it treats blank lines as delimiters.
-
Like "
$,
" except that it applies to list values interpolated into a double-quoted string (or similar interpreted string). Default is a space. -
The subscript separator for multidimensional array emulation. Default is
"\034"
. -
What a format outputs to perform a formfeed. Default is
"\f"
. -
The current set of characters after which a string may be broken to fill continuation fields (starting with
^
) in a format. Default is"\n""
. -
The current value of the
write
accumulator forformat
lines. -
Contains the output format for printed numbers (deprecated).
-
The status returned by the last pipe close, backtick (
``
) command, orsystem
operator. -
If used in a numeric context, yields the current value of the
errno
variable, identifying the last system call error. If used in a string context, yields the corresponding system error string. -
Contains the name of the file containing the Perl script being executed.
-
The index of the first element in an array and of the first character in a substring. Default is 0.
-
Contains internal compiler hints enabled by certain pragmatic modules.
-
The current value of the inplace-edit extension. Use
undef
to disable inplace editing. -
The contents of
$M
can be used as an emergency memory pool in case Perldie
s with an out-of-memory error. Use of$M
requires a special compilation of Perl. See the INSTALL document for more information. -
Contains the name of the operating system that the current Perl binary was compiled for.
-
The internal flag that the debugger clears so that it doesn't debug itself.
-
The time at which the script began running, in seconds since the epoch.
-
The current value of the warning switch, either true or false.
-
Contains the name of the current file when reading from
<ARGV>
.
$_
$ARG
$.
$INPUT_LINE_NUMBER
$NR
$/
$INPUT_RECORD_SEPARATOR
$RS
$,
$OUTPUT_FIELD_SEPARATOR
$OFS
$\
$OUTPUT_RECORD_SEPARATOR
$ORS
$
$LIST_SEPARATOR
$;
$SUBSCRIPT_SEPARATOR
$SUBSEP
$^L
$FORMAT_FORMFEED
$:
$FORMAT_LINE_BREAK_CHARACTERS
$^A
$ACCUMULATOR
$#
$OFMT
$?
$CHILD_ERROR
$!
$OS_ERROR
$ERRNO
$@
$EVAL_ERROR
$$
$PROCESS_ID
$PID
$<
$REAL_USER_ID
$UID
$>
$EFFECTIVE_USER_ID
$EUID
$(
$REAL_GROUP_ID
$GID
$)
$EFFECTIVE_GROUP_ID
$EGID
$0
$PROGRAM_NAME
$[
$]
$PERL_VERSION
$^D
$DEBUGGING
$^E
$EXTENDED_OS_ERROR
$^F
$SYSTEM_FD_MAX
$^H
$^I
$INPLACE_EDIT
$^M
$^O
$OSNAME
$^P
$PERLDB
$^T
$BASETIME
$^W
$WARNING
$^X
$EXECUTABLE_NAME
$ARGV
4.4.2 Global Special Arrays and Hashes
-
The array containing the command-line arguments intended for the script.
-
The array containing the list of places to look for Perl scripts to be evaluated by the
do
,require
, oruse
constructs. -
The array into which the input lines are split when the -a command-line switch is given.
-
The hash containing entries for the filename of each file that has been included via
do
orrequire
.
@ARGV
@INC
@F
%INC
%ENV
%SIG
4.4.3 Global Special Filehandles
-
The special filehandle that iterates over command line filenames in
@ARGV
. Usually written as the null filehandle in<>
. -
The special filehandle that refers to anything following the
__END__
token in the file containing the script. Or, the special filehandle for anything following the__DATA__
token in a required file, as long as you're reading data in the same package__DATA__
was found in. -
The special filehandle used to cache the information from the last
stat
,lstat
, or file test operator.
ARGV
STDERR
STDIN
STDOUT
DATA
_
(underscore)
4.4.4 Global Special Constants
-
Indicates the logical end of your program. Any following text is ignored, but may be read via the
DATA
filehandle. -
Represents the filename at the point in your program where it's used. Not interpolated into strings.
-
Represents the current line number. Not interpolated into strings.
-
Represents the current package name at compile time, or undefined if there is no current package. Not interpolated into strings.
__END__
__FILE__
__LINE__
__PACKAGE__
4.4.5 Regular Expression Special Variables
For more information on regular expressions, see Section 4.6, "Regular Expressions" later in this chapter.
-
Contains the text matched by the corresponding set of parentheses in the last pattern matched. For example,
$1
matches whatever was contained in the first set of parentheses in the previous regular expression. -
The string preceding whatever was matched by the last successful pattern match.
-
The string following whatever was matched by the last successful pattern match.
-
The last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns was matched. For example:
/Version: (.*)|Revision: (.*)/ && ($rev = ___FCKpd___1);
$
digit
$&
$MATCH
$`
$PREMATCH
$'
$POSTMATCH
$+
$LAST_PAREN_MATCH
4.4.6 Filehandle Special Variables
Most of these variables only apply when using formats. See Section 4.10, "Formats" later in this chapter.
-
If set to nonzero, forces an
fflush(3)
after everywrite
orprint
on the currently selected output channel. -
The current page number of the currently selected output channel.
-
The current page length (printable lines) of the currently selected output channel. Default is 60.
-
The number of lines left on the page of the currently selected output channel.
-
The name of the current report format for the currently selected output channel. Default is the name of the filehandle.
-
The name of the current top-of-page format for the currently selected output channel. Default is the name of the filehandle with
_TOP
appended.
$|
$OUTPUT_AUTOFLUSH
$%
$FORMAT_PAGE_NUMBER
$=
$FORMAT_LINES_PER_PAGE
$-
$FORMAT_LINES_LEFT
$~
$FORMAT_NAME
$^
$FORMAT_TOP_NAME