Global Special Variables

There are quite a few variables that are global in the fullest sense -- they mean the same thing in every package. If you want a private copy of one of them, you must localize it in the current block.

Variable Contents Mnemonic

$_ The default input and pattern-searching space. The following pairs are equivalent: while (<>) {... # equivalent only in while!

while ($_ =<>) {... /^Subject:/

$_ =~ /^Subject:/ y/a-z/A-Z/

$_ =~ y/a-z/A-Z/ chop

chop($_) underline is understood to be underlying certain undertakings

$. The current input line number of the last filehandle that was read. Rember that only an explicit close on the filehandle resets the line number. many programs use . to mean the current line number

$/ The input record separator, newline by default. $/ may be set to a value longer than one character in order to match a multi-character delimiter. If $/ is undefined, no record separator is matched, and <FILEHANDLE> will read everything to the end of the current file. / is used to delimit line boundries when quoting poetry. Or, if you prefer, think of mad slashers cutting things to ribbons.

$\ The output record separator for the print operator. You set $\ instead of adding

at the end of the print.

$, The output field separator for the print operator. What is printed when there is a , in your print statement

$" This is similar to $, except that it applies to array values interpolated into a double-quoted string (or similar interpreted string). Default is space. Obvious, I think

$# The output format for numbers display via the print operator # is the number sign

$$ The process number of the Perl running this script Same as shells

$? The status returned by the last pipe close, backtick(``) command or system operator. Note that this is the status word returned by the wait() system call, so the exit value of the subprocess is actually ($? >>*). $? & 255 gives which signal, if any, the process died from, and whether there was a core dump. Similar to sh and ksh

$* Set to 1 to do multi-line matching within a string, 0 to tell Perl that it can assume that strings contain a single line, for the purpose of optimizing pattern matches. Default is 0 * matches multiple things

$0 Contains the name of the file containing the Perl script being executed. Depending on your OS, it may or may not include the full pathname. Same as sh and ksh

$[ The index of the first element in an array, and of the first character in a substring. [ begins subscripts

$] The first part of the string printed out when you say perl -v. It can be used to determine at the beginning of a script whether the Perl interpreter executing the script is in the right range of versions. If used in a numeric context, $] returns version + patchlevel /1000. Is this version of Perl in the "rightbracket"?

$; The subscript separator for multi-dimensional array emulation. If you refer to an associative array element as: $foo{$a,$b,$c}

it really means:

$foo{join($;, $a, $b, $c)}

but don't put

@foo{$a,$b,$c}

which means

($foo{$a},$foo{$b},$foo{$c}) Comma (the syntactic subscript separator) is a semi-semicolon. Yeah, it's pretty lame, but $, is already taken for something more important.

$! If used in a numeric context, yields the current value of errno, with all the usual caveats. (This means that you shouldn't depend on the value of $! to be anything in particular unless you've gotten a specific error return indicating a system error.) If used in a string context, yields the corresponding sysem error string. What just went bang?