Special Variables

Table of Contents

Please bear with me while I try to complete these pages. I'll get to them when I can.
Cheers...


Per-filehandle Special Variables
These variables never need to be mentioned in a local()because they always refer to some value pertaining to the currently selected output filehandle - each filehandle keeps its own set of values.
Variable Contents Mnemonic
$| If set to nonzero, forces a flush after every write or print When you want your pipes to be piping hot
$% Current page number % is page number in nroff
$= Current page length = has horizontal lines
$- Number of lines left on the page lines_on_page - lines_printed
$~ Name of the current report format Closely related to $^
$^ Name of the current top-of-page format Points to top of page

Return to Top


Local Special Variables
These variables that are always local to the current block, so you never need to mention them in a local(). All of them are associated with the last successful pattern match.
Variable Contents Mnemonic
$1..$9 Contains the subpattern from the corresponding set of parentheses in the last pattern matched like \1..\9
$& Contains the string matched by the last pattern match like & in some editors
$` The string preceding whatever was matched by the last pattern match, not counting patterns matched in nested blocks that have been exited already. ` often precedes a quoted string in normal text
$' The string following whatever was matched by the last pattern match, not counting patterns matched in nested blockes that have been exited already. For example:

   $_ = 'abcdefghi';
   /def/;
   print "$`:$&:$'\n";    # prints abc:def:ghi

' often follows a quoted string in normal text
$+ the last bracket matched by the last search pattern. This is useful if you don't know which of a set of alternative patterns matched. For example:

    /Version: (.*)|Revision: (.*)/ && ($rev = $+);

be positive and forward looking

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 \n 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?
$@ The Perl syntax error or routine error message from the last eval, do-FILE, or require command. If set, either the compilation failed, or the die function was executed within the code of the eval. Where was the syntax error at?

Return to Top


1

1