Some special variables are defined in the Perl language, 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 special variables with English names, you need to add use English; at the head of the program. This allows the use of descriptive English special variables.
The most commonly used special variable is $_, which contains default input and pattern matching content. Examples are as follows:
Executing the above program, the output result is:
GoogleCoderctoTaobao
In the following examples we do not use $_ to output content:
Executing the above program, the output result is:
GoogleCoderctoTaobao
In the example, "Google" is output first, then "Codercto" is output, and finally "Taobao" is output.
In an iterative loop, the string of the current loop will be placed in $_, and then output through print. In addition, print does not specify an output variable, and $_ is used by default.
Here are a few places where Perl will assume $_ even if it's not specified:
Various unary functions, including functions like ord() and int() and all file test operations ("-f", "-d") except "-t", which operates on STDIN by default.
Various list functions such as print() and unlink().
Pattern matching operations "m//", "s///" and "tr///" without the "=~" operator.
The default iteration variable for "foreach" loops when no other variables are given.
Implicit iteration variables for grep() and map() functions.
When "while" has only one condition, and that condition tests the result of the "" operation, $_ is the default location for the input record. This does not happen except in "while" test conditions. (Mnemonic: The underscore can be omitted in certain operations.)
According to the nature of use of special variables, they can be divided into the following categories:
Global scalar special variable.
Global array special variables.
Global hash special variable.
Global special file handle.
Global special constants.
Regular expression special variables.
File handle special variable.
The following lists all scalar special variables, including variables with special characters and English forms:
$_ | Default input and pattern matching content. |
$ARG | |
$. | The current line number of the file handle last read |
$NR | |
$/ | Enter the record separator, which defaults to a new line character. If you use the undef variable, the end of the file will be read. |
$RS | |
$, | Output field separator |
$OFS | |
$ | Output record separator |
$ORS | |
$" | This variable is similar to $, but should be used to interpolate array and slice values into double-quoted strings (or similar interpolated strings). Defaults to one space. |
$LIST_SEPARATOR | |
$; | Delimiter used when simulating multidimensional arrays. The default is " |