Perl special variables
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 a special variable with an English name, 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. The example is as follows:
#!/usr/bin/perl foreach ('Google','php','Taobao') { print $_; print "\n"; }
Execute the above program, the output result is:
Google php Taobao
We do not use $_ to output the content in the following example:
#!/usr/bin/perl foreach ('Google','php','Taobao') { print; print "\n"; }
Execute the above program, the output result is: :
Google php Taobao
In the example, "Google" is output first, then "php" is output, and "Taobao" is finally 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.
The following are several places where Perl will assume $_ even if it is not specified:
Various unary functions, including ones like ord() and int( ) Such functions and all file test operations ("-f", "-d") except "-t", which operates STDIN by default.
Various list functions, such as print() and unlink().
Pattern matching operations "m//", "s///" and "tr///" when the "=~" operator is not used.
is the default iteration variable for "foreach" loops when no other variables are given.
Implicit iteration variable for grep() and map() functions.
When "while" has only one condition, and the condition tests the result of the "" operation, $_ is the default location to store the input record. This does not happen except in "while" test conditions. (Mnemonic: Underscores can be omitted in specific operations.)
Special variable types
According to the nature of use of special variables, they can be divided into The following categories:
Global scalar special variables.
Global array special variables.
Global hash special variable.
Global special file handle.
Global special constants.
Regular expression special variable.
File handle special variable.
Global scalar special variables
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, the default is the new line character. If you use the undef variable, the end of the file will be read. |
$RS | |
$, | Output domain delimiter |
$OFS | |
Output record delimiter | |
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 a space . | |
##$; | |
$SUBSCRIPT_SEPARATOR | |
$^L | |
$FORMAT_FORMFEED | |
##$: | |
##$FORMAT_LINE_BREAK_CHARACTERS | |
Variable used to save formatted data before printing | |
$ACCUMULATOR | |
# Default number output format when printing numbers (deprecated). | |
$OFMT | |
$? | Return the status of the previous external command |
$CHILD_ERROR | |
The numerical value of this variable is the value of errno, and the string value is the corresponding system error string | |
The error message of command eval. If it is empty, it means that the last eval command was executed successfully. | |
The process number running the current Perl script | |
The actual user ID of the current process | |
Effective user ID of the current process | |
The actual group user ID of the current process | |
The effective group user ID of the current process | |
Contains the file name of the script being executed | |
Array of array The subscript of an element, which defaults to 0. | |
Perl version number | |
Value of debugging flag | |
Operating system extended error messages in non-UNIX environments | |
Maximum file bundle descriptor value | |
##$^H | |
$INPLACE_EDIT | |
$^M | |
$^O | Operating system name |
$OSNAME | |
Internal variable specifying the current debug value | |
Starting from the new century, the time the script starts running in seconds | |
The current value of the warning switch | |
The name of the Perl binary executable code | |
The current file name when reading from the default file handle | |
@INC | |
@F | |
The hash table %INC contains all files included with do or require statements. The keyword is the file name and the value is the path of the file | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Contains the current environment variables | |||||||||||||||||||||||||||||||||||||||||||||||||||||
Signal list and its processing Way |
ARGV | Traverse the special file handle of all file names in the array variable @ARGV |
STDERR | Standard error output handle |
STDIN | Standard input handle |
STDOUT | Standard output handle |
DATA | Special file handles refer to anything in the file after the __END__ flag, including script content. Or reference everything after the __DATA__ flag in an include file. As long as you read data in the same package, __DATA__ exists. |
_ (underscore) | Special file handles are used to cache file information (fstat, stat, and lstat). |
Global special constants
##__END__ | Logical end of script , ignore the following text.|
__FILE__ | Current file name|
__LINE__ | Current line number|
__PACKAGE__ | Current package name, the default package name is main.
$n | contains The nth substring of the last pattern match||||||||||||||||||
$& | The string of the previous successful pattern match||||||||||||||||||
$MATCH | ||||||||||||||||||
$` | The content before the last successfully matched substring||||||||||||||||||
$PREMATCH | ||||||||||||||||||
The previous match was successful Content after the substring | ||||||||||||||||||
The last bracket matching the previous regular expression search pattern. For example: | ||||||||||||||||||
$| | If set to zero, after each call to the function write or print , automatically calls the function fflush, and writes the written content back to the file |
$OUTPUT_AUTOFLUSH | |
$% | Current output page number |
$FORMAT_PAGE_NUMBER | |
$= | Current length of each page. Default is 60. |
$FORMAT_LINES_PER_PAGE | |
##$- | The remainder of the current page Number of lines|
$FORMAT_LINES_LEFT | |
$~ | Current report The name of the output format. The default value is the file handle name.|
$FORMAT_NAME | |
Current report output table The name of the header format. The default value is the file handle name with the suffix "_TOP". | |