home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeJava and XSLTSearch this book

4.4. Special Variables

Some variables have a predefined, special meaning in Perl. They use punctuation characters after the usual variable indicator ($, @, or %), such as $_. The explicit, long-form names 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 common special variable is $_, which contains the default input and pattern-searching string. For example:

foreach ('hickory','dickory','doc') {

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 in $_ 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 such as ord and int, as well as the all file tests (-f, -d), except for -t, which defaults to STDIN.

  • Various list functions such as print and unlink.

  • The pattern-matching operations m//, s///, and tr/// 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 and map 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 a while test, this does 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.

The output field separator for the print operator.

The output record separator for the print operator.

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 for format lines.

Contains the output format for printed numbers (deprecated).

The status returned by the last pipe close, backtick ('') command, or system 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.

The Perl syntax error message from the last eval command.

The pid of the Perl process running this script.

The real user ID (uid) of this process.

The effective uid of this process.

The real group ID (gid) of this process.

The effective gid of this process.

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.

Returns the version plus patch level divided by 1,000.

The current value of the debugging flags.

Extended error message on some platforms.

The maximum system file descriptor, ordinarily 2.

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 Perl die 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 for which the current Perl binary was compiled.

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.

The name that the Perl binary itself was executed as. As of Perl 5.8, Perl asks the operating system instead of using C's argv[0].

Contains the name of the current file when reading from <ARGV>.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.