Perl Predefined Variables
Variable Description Example
$ARG
$_
The default input and pattern-searching space.
(Mnemonic: underline is understood in certain
operations.)
while (<>) {...} #equiv. only in while
while (defined($_ = <>)) {...}
$a
$b
Special package variables when using sort(), see
sort.
@articles = sort {$a cmp $b} @files;
$<digits>
Contains the sub-pattern from the corresponding
set of capturing parentheses from the last pattern
match. (Mnemonic: like \digits.)
$MATCH
$&
The string matched by the last successful pattern
match. (Mnemonic: like & in some editors.)
$PREMATCH
$`
The string preceding whatever was matched by the
last successful pattern match. (Mnemonic: ` often
precedes a quoted string.)
$POSTMATCH
$’
The string following whatever was matched by the
last successful pattern match. (Mnemonic: ' often
follows a quoted string.)
local $_ = 'abcdefghi';
/def/;
print "$` : $& : $'", "";
# prints abc : def : ghi
$LAST_PATTERN
_MATCH
$+
The text matched by the last bracket of the last
successful search pattern.
This is useful if you don't know which one of a set
of alternative patterns matched. (Mnemonic: be
positive and forward looking.)
/Version: (.*)|Revision: (.*)/
&&
($rev = $+);
$^N
The text matched by the used group most-recently
closed (i.e. the group with the rightmost closing
parenthesis) of the last successful search pattern.
(Mnemonic: the (possibly) Nested parenthesis that
most recently closed.)
$v = "sep:2:match";
$v =~ /(?:(\d)(?{ $a = $^N }))/;
print $a; # prints 2
@LAST_MATCH_END
@+
This array holds the offsets of the ends of the last
successful submatches in the currently active
dynamic scope.
$+[0] is the offset into the string of
the end of the entire match.
$+[1] is the offset past where $1
ends.
You can use $#+ to determine how many
subgroups were in the last successful
match.
$*
Set to a non-zero integer value to do multi-line
matching within a string, 0 (or undefined) to tell
Perl that it can assume that strings contain a single
line, for the purpose of optimizing pattern
matches. (Mnemonic: * matches multiple things.)
Use of $* is deprecated in modern
Perl, supplanted by the /s and /m
modifiers on pattern matching.
HANDLE->
input_line_number(EXPR)
$INPUT_LINE_NUMBER
$NR
$.
Current line number for the last filehandle
accessed. (Mnemonic: many programs use "." to
mean the current line number.)
IO::Handle->
input_record_separator
(EXPR)
$INPUT_RECORD
_SEPARATOR
$RS
$/
The input record separator, newline by default.
Setting $/ to a reference to an integer, scalar
containing an integer, or scalar that's convertible
to an integer will attempt to read records instead of
lines, with the maximum record size being the
referenced integer. (Mnemonic: / delimits line
boundaries when quoting poetry.)
local $/; # enable "slurp" mode
local $_ = <FH>; # whole file now here
HANDLE->
autoflush(EXPR)
$OUTPUT_AUTOFLUSH
$|
If set to nonzero, forces a flush right away and
after every write or print on the currently selected
output channel. Default is 0. (Mnemonic: when
you want your pipes to be piping hot.)
IO::Handle->
output_field_separator
(EXPR)
$OUTPUT_FIELD
_SEPARATOR
$OFS
$,
The output field separator for the print operator. If
defined, this value is printed between each of
print's arguments. Default is undef. (Mnemonic:
what is printed when there is a "," in your print
statement.)
@arr = (1,2,3);
$, = “ - ”
print @arr; # prints 1 – 2 - 3
IO::Handle->
output_record_separator
(EXPR)
$OUTPUT_RECORD
_SEPARATOR
$ORS
$\
The output record separator for the print operator.
Default is undef.
(Mnemonic: you set $\ instead of adding "\n" at
the end of the print.)
@arr = (1, 2, “baz”);
$\ = “\t”
foreach (@arr) { print }
# prints 1 [tab] 2 [tab] baz
good coders code, great reuse