Perl Special Variables
The Perl language defines some special variables, usually prefixed with $, @, or %, for example: $_.
Many special variables have a long English name; the operating system variable $! can be written as $OS_ERROR.
If you want to use the English names of special variables, you need to add at the top of the program use English;. This way you can use descriptive English special variable names.
The most commonly used special variable is $_, which contains the default input and pattern matching content. An example is as follows:
Example
#!/usr/bin/perl
foreach
(
'
'
,
'
Runoob
'
,
'
Taobao
'
)
{
$_
;
"
\n
"
;
}
Running the above program, the output is:
Google
Runoob
Taobao
In the following example we do not use $_ to output content:
Example
#!/usr/bin/perl
foreach
(
'
'
,
'
Runoob
'
,
'
Taobao
'
)
{
;
"
\n
"
;
}
Running the above program, the output is:
Google
Runoob
Taobao
In the example, "Google" is output first, then "Runoob", and finally "Taobao".
In an iteration loop, the current string of the loop is placed in $_ and then output through print. In addition, when print is not given an output variable, by default it also uses $_.
The following are several places where Perl assumes the use of $_ even if it is not explicitly written:
- Various unary functions, including functions like ord() and int(), and all file test operations except "-t" ("-f", "-d"); "-t" operates on STDIN by default.
- Various list functions, such as print() and unlink().
- Pattern matching operations "m//", "s///", and "tr///" when the "=~" operator is not used.
- The default iteration variable of a "foreach" loop when no other variable is given.
- The implicit iteration variable of the grep() and map() functions.
- When "while" has only a single condition, and that condition tests the result of the <> operation, $_ is the default location where the input record is stored. This does not happen outside the "while" test condition. (Mnemonic: the underscore can be omitted in certain operations.)
Types of Special Variables
According to their usage, special variables can be divided into the following categories:
-
Global scalar special variables.
-
Global array special variables.
-
Global hash special variables.
-
Global special file handles.
-
Global special constants.
-
Regular expression special variables.
-
File handle special variables.
Global Scalar Special Variables
The following lists all the scalar special variables, including both the special character and English forms:
| $_ | Default input and pattern matching content. |
|---|---|
| $ARG | |
| $. | The current line number of the last file handle read |
| $NR | |
| $/ | The input record separator, default is the newline character. If this variable is set to undef, it will read to the end of the file. |
| $RS | |
| $, | The output field separator |
| $OFS | |
| $\ | The output record separator |
| $ORS | |
| $" | This variable is similar to the $, variable, but applies when interpolating arrays and slice values into double-quoted strings (or similar interpolated strings). It defaults to a space. |
| $LIST_SEPARATOR | |
| $; | The separator used when simulating multidimensional arrays. Defaults to "\034". |
| $SUBSCRIPT_SEPARATOR | |
| $^L | The form feed character sent to the output channel. Defaults to "\f". |
| $FORMAT_FORMFEED | |
| $: | The current set of characters after which a string may be broken to fill continuation fields (starting with ^) in a format. Default is "\n"". |
| $FORMAT_LINE_BREAK_CHARACTERS | |
| $^A | The variable used to hold formatted data before printing |
| $ACCUMULATOR | |
| $# | The default numeric output format when printing numbers (deprecated). |
| $OFMT | |
| $? | Returns the status of the last external command |
| $CHILD_ERROR | |
| $! | The numeric value of this variable is the value of errno, and its string value is the corresponding system error string |
| $OS_ERROR or $ERRNO | |
| $@ | The error message of the eval command. If empty, it means the last eval command executed successfully |
| $EVAL_ERROR | |
| $$ | The process ID of the currently running Perl script |
| $PROCESS_ID or $PID | |
| $< | The real user ID of the current process |
| $REAL_USER_ID or $UID | |
| $> | The effective user ID of the current process |
| $EFFECTIVE_USER_ID or $EUID | |
| $( | The real group ID of the current process |
| $REAL_GROUP_ID or $GID | |
| $) | The effective group ID of the current process |
| $EFFECTIVE_GROUP_ID or $EGID | |
| $0 | Contains the filename of the script being executed |
| $PROGRAM_NAME | |
| $[ | The subscript of the first element of an array, default is 0. |
| $] | The version number of Perl |
| $PERL_VERSION | |
| $^D | The value of the debugging flag |
| $DEBUGGING | |
| $^E | Operating system extended error information in non-UNIX environments |
| $EXTENDED_OS_ERROR | |
| $^F | The maximum file descriptor number |
| $SYSTEM_FD_MAX | |
| $^H | The syntax check state activated by the compiler |
| $^I | The value of the built-in control editor |
| $INPLACE_EDIT | |
| $^M | The size of the alternate memory pool |
| $^O | Operating system name |
| $OSNAME | |
| $^P | An internal variable that specifies the current debugging value |
| $PERLDB | |
| $^T | The time in seconds since the start of the century that the script began running |
| $BASETIME | |
| $^W | The current value of the warning switch |
| $WARNING | |
| $^X | The name of the Perl binary executable |
| $EXECUTABLE_NAME | |
| $ARGV | The current filename when reading from the default file handle |
Global Array Special Variables
| @ARGV | The list of command-line arguments passed to the script |
|---|---|
| @INC | The list of directories to search when importing modules |
| @F | Array input from the command line |
Global Hash Special Variables
|
%INC |
The hash %INC contains all files included with do or require statements. The key is the filename, and the value is the path to that file |
|---|---|
|
%ENV |
Contains the current environment variables |
|
%SIG |
The list of signals and how they are handled |
Global Special File Handles
|
ARGV |
A special file handle that iterates over all the filenames in the array variable @ARGV |
|---|---|
|
STDERR |
Standard error output handle |
|
STDIN |
Standard input handle |
|
STDOUT |
Standard output handle |
|
DATA |
The special file handle references anything in the file after the END marker that contains the script content. Or it references all content in the file after the __DATA__ marker, as long as you read the data in the same package,DATA it exists. |
|
_ (underscore) |
A special file handle used to cache file information (fstat, stat, and lstat). |
Global Special Constants
|
END |
The logical end of the script; any text after it is ignored. |
|---|---|
|
FILE |
Current file name |
|
LINE |
The current line number |
|
PACKAGE |
The current package name; the default package name is main. |
Regular Expression Special Variables
|
$n |
Contains the nth substring from the last pattern match |
|---|---|
|
$& |
The string from the last successful pattern match |
|
$MATCH |
|
|
$` |
The content before the substring of the last successful match |
|
$PREMATCH |
|
|
$' |
The content after the substring of the last successful match |
|
$POSTMATCH |
|
|
$+ |
The last bracket that matched in the previous regex search pattern. For example: /Version: (.)|Revision: (.)/ && ($rev = $+); |
|
$LAST_PAREN_MATCH |
File Handle Special Variables
|
$| |
If set to zero, after each call to write or print, the fflush function is automatically called to write the content back to the file |
|---|---|
|
$OUTPUT_AUTOFLUSH |
|
|
$% |
Current output page number |
|
$FORMAT_PAGE_NUMBER |
|
|
$= |
Current page length. Defaults to 60. |
|
$FORMAT_LINES_PER_PAGE |
|
|
$- |
The number of lines remaining on the current page |
|
$FORMAT_LINES_LEFT |
|
|
$~ |
The name of the current report output format. The default is the file handle name. |
|
$FORMAT_NAME |
|
|
$^ |
The name of the current report output header format. The default is the file handle name with the suffix "_TOP". |
|
$FORMAT_TOP_NAME |