Perl Predefined Variables (Special Variable) Cheat Sheet
Perl Predefined Variables (Special Variable) Cheat Sheet
© 2007 Peteris Krumins [email protected] http://www.catonmat.net good coders code, great reuse
This is like $, except that it applies to array and
@arr = (“foo”, “esr”, “rms”);
$LIST_SEPARATOR slice values interpolated into a double-quoted $” = “ - ”
$" string (or similar interpreted string). Default is a print “@arr”; # prints foo – esr – rms
space.
The subscript separator for multidimensional array
$SUBSCRIPT_SEPARATOR If you refer to a hash element as
emulation. Default is "\034", the same as SUBSEP
$SUBSEP $foo{$a,$b,$c} it really means
in awk. (Mnemonic: comma (the syntactic $foo{join($;, $a, $b, $c)}
$; subscript separator) is a semi-semicolon.)
The output format for printed numbers. This
variable is a half-hearted attempt to emulate awk's
OFMT variable. The initial value is "%.ng", where
$#
n is the value of the macro DBL_DIG from your
system's float.h. (Mnemonic: # is the number
sign.)
HANDLE->
format_page_number The current page number of the currently selected
(EXPR) output channel. Used with formats. (Mnemonic: %
$FORMAT_PAGE_NUMBER is page number in nroff.)
$%
HANDLE->
format_lines_per_page The current page length (printable lines) of the
(EXPR) currently selected output channel. Default is 60.
$FORMAT_LINES_PER_PA Used with formats. (Mnemonic: = has horizontal
GE lines.)
$=
HANDLE-> The number of lines left on the page of the
format_lines_left(EXPR) currently selected output channel. Used with
$FORMAT_LINES_LEFT formats. (Mnemonic: lines_on_page -
$- lines_printed.)
$` is same as substr($var, 0, $-[0])
$& is the same as substr($var, $-[0],
$+[0] - $-[0])
$-[0] is the offset of the start of the last successful $' is the same as substr($var, $+[0])
@LAST_MATCH_START match. $-[n] is the offset of the start of the $1 is the same as substr($var, $-[1],
@- substring matched by n-th subpattern, or undef if $+[1] - $-[1])
the subpattern did not match. $2 is the same as substr($var, $-[2],
$+[2] - $-[2])
$3 is the same as substr($var, $-[3],
$+[3] - $-[3])
HANDLE-> The name of the current report format for the
format_name(EXPR) currently selected output channel. Default is the
$FORMAT_NAME name of the filehandle.
$~ (Mnemonic: brother to $^ .)
HANDLE-> The name of the current top-of-page format for the
format_top_name(EXPR) currently selected output channel. Default is the
$FORMAT_TOP_NAME name of the filehandle with _TOP appended.
$^ (Mnemonic: points to top of page.)
IO::Handle->
The current set of characters after which a string
format_line_break
may be broken to fill continuation fields (starting
_characters(EXPR)
with ^) in a format. Default is " \n-", to break on
$FORMAT_LINE_BREAK whitespace or hyphens. (Mnemonic: a "colon" in
_CHARACTERS poetry is a part of a line.)
$:
IO::Handle->
format_formfeed(EXPR)
What formats output as a form feed. Default is \f.
$FORMAT_FORMFEED
$^L
The current value of the write() accumulator for
format() lines. A format contains formline() calls
that put their result into $^A . After calling its
$ACCUMULATOR
format, write() prints out the contents of $^A and
$^A
empties. So you never really see the contents of
$^A unless you call formline() yourself and then
look at it.
The exit value of the subprocess is
The status returned by the last pipe close, backtick really ($?>>8), and $? & 127 gives
$CHILD_ERROR
(`` ) command, successful call to wait() or which signal, if any, the process died
$? from, and $? & 128 reports whether
waitpid(), or from the system() operator.
there was a core dump.
The object reference to the Encode object that is
${^ENCODING} used to convert the source code to Unicode.
Default is undef.
© 2007 Peteris Krumins [email protected] http://www.catonmat.net good coders code, great reuse
if (open(FH, $filename)) {
# Here $! is meaningless.
If used numerically, yields the current value of the ...
$OS_ERROR
C errno variable, or in other words, if a system or } else {
$ERRNO # ONLY here is $! meaningful.
library call fails, it sets this variable. (Mnemonic:
$! What just went bang?) ...
# Here $! might be meaningless.
}
For example, $!{ENOENT} is true if and
Each element of %! has a true value only if $! is only if the current value of $! is
%! ENOENT ; that is, if the most recent
set to that value.
error was "No such file or directory"
$EXTENDED_OS_ERROR Error information specific to the current operating
$^E system. (Mnemonic: Extra error explanation.)
The Perl syntax error message from the last eval()
$EVAL_ERROR
operator. (Mnemonic: Where was the syntax error
$@ "at"?)
$PROCESS_ID
The process number of the Perl running this script.
$PID
(Mnemonic: same as shells.)
$$
$REAL_USER_ID
The real uid of this process. (Mnemonic: it's the
$UID
uid you came from, if you're running setuid.)
$<
$< = $>; # set real to effective uid
$EFFECTIVE_USER_ID # swap real and effective uid
The effective uid of this process. (Mnemonic: it's
$EUID ($<,$>) = ($>,$<);
the uid you went to, if you're running setuid.)
$>
© 2007 Peteris Krumins [email protected] http://www.catonmat.net good coders code, great reuse