Perl special variable
The Perl language defines some special variables, usually prefixed with $
, @
, or %
, such as $_
.
Many special variables have a long English name, 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 it to the header of the program. use English;
. In this way, descriptive English special variables can be used.
The most commonly used special variables are $_
which contains the default input and pattern matching content Examples are as follows:
Example
#!/usr/bin/perlforeach('Google','Runoob','Taobao'){print$\_;print"\\n";}
Execute the above program, and the output is as follows:
Google
Runoob
Taobao
In the example, first output “Google”, then “Runoob”, and finally “Taobao”.
Example
#!/usr/bin/perlforeach('Google','Runoob','Taobao'){print;print"\\n";}
Execute the above program, and the output is as follows:
Google
Runoob
Taobao
In the example, first output “Google”, then “Runoob”, and finally “Taobao”.
In an iteration loop, the string of the current loop is placed in the $\_
and then pass through the print
output. In addition, print
when no output variable is specified, it is also used by default $_
.
Here are a few places where Perl is assumed to be used even if it is not specified $\_
the place:
Various monocular functions, including things like
ord()
andint()
such functions and all file test operations except “- t” (”- f”, “- d”), “- t” default operation STDIN.Various list functions, such as
print()
andunlink()
.Pattern matching operations “m _”, “_” and “tr///” when the “= ~” operator is not used.
Is the default iteration variable for the “foreach” loop when no other variables are given.
grep()
andmap()
implicit iteration variable of the function.When “while” has only a unique condition, and the condition is to test the result of the
""
operation.$\_
is the default location where inputrecords are stored. This does not happen except for the “while” test condition. Mnemonic: underlining can be omitted in specific operations.)
Special variable type
According to the nature of the use of special variables, they can be dividedinto the following categories:
Global scalar special variables.
Global array special variables.
Global hash special variable.
Global special file handle.
Global special constant.
Regular expression special variables.
File handle special variable.
Global scalar special variable
All the scalar special variables, including special characters and variablesin English form, are listed below:
|
The default input and pattern match content. |
|
|
|
The current line number of the last read file handle |
|
|
|
Enter a record delimiter, which defaults to new line characters. If you use the variable undef, it will be read to the end of the file. |
|
|
|
Output domain delimiter |
|
|
|
Output record delimiter |
|
|
|
This variable is similar to $, but applies to interpolating array and slice values into strings (or similar interpolated strings) caused by double quotes. The default is a space. |
|
|
|
The delimiter used when simulating multidimensional arrays. The default is “034”. |
|
|
|
The feed feed character sent to the output channel. The 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”. |
|
|
|
Variables used to save formatted data before printing |
|
|
|
The default digital output format (obsolete) when printing numbers. |
|
|
|
Returns the status of the previous external command |
|
|
|
The numeric value of this variable is the value of errno, and the string value is the corresponding system error string |
|
|
|
Error message for command eval. If empty, it indicates successful execution of the previous eval command |
|
|
|
The process number that runs the current Perl script |
|
|
|
Actual user number of the current process |
|
|
|
The valid user number of the current process |
|
|
|
The actual group user number of the current process |
|
|
|
The valid group user number of the current process |
|
|
, 0 |
The file name that contains the script being executed |
|
|
|
The subscript of the first element of the array. The default is 0. |
|
The version number of Perl |
|
|
|
The value of the debugging flag |
|
|
|
Operating system extension error message in non-UNIX environment |
|
|
|
Maximum file bundle descriptor value |
|
|
|
Syntax check status activated by the compiler |
|
Value of the built-in control editor |
|
|
|
Size of the spare memory pool |
|
Operating system name |
|
|
|
Specifies the internal variable of the current debug value |
|
|
|
The time to start running in seconds from the beginning of the new century. |
|
|
|
The current value of the warning switch |
|
|
|
The name of the Perl binary executable code |
|
|
|
The current file name when read from the default file handle |
Global array special variables
@ ARGV |
List of command line arguments passed to the script |
@ INC |
List of directories to search when importing a module |
@ F |
Array input on the command line |
Global hash special variable
|
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 to this file |
|
Contains the current environment variable |
|
Signal list and its processing method |
Global special file handle
|
Special file handles for traversing all file names in the array variable @ ARGV |
|
Standard error output handle |
|
Standard input handle |
|
Standard output handle |
|
Special file handles are referenced in the file__ END__ any content after the flag contains script content. Alternatively, refer to all content after the __DATA__ flag in an include file, as long as you have read data in the same package __DATA__ |
|
Special file handles are used to cache file information (fstat, stat, and lstat). |
Global special constant
__END__ |
The logical end of the script, ignoring the following text. |
__FILE__ |
Current file name |
__LINE__ |
Current line number |
__PACKAGE__ |
Current package name, the default package name is main. |
Regular expression special variable
|
Contains the nth substring of the last pattern match |
|
The string of the previous successful pattern match |
|
|
|
The content before the previous successful substring match |
|
|
|
The content after the previous successful matching substring |
|
|
|
The last parenthesis that matches the previous regular expression search format. For example: |
|
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
|
File handle special variable
|
If set to zero, after each call to the function write or print, the function fflush is automatically called to write the written content back to the file |
|
|
|
Current output page number |
|
|
|
The current length per page. The default is 60. |
|
|
|
The number of rows left on the current page |
|
|
|
The name of the current report output format. The default value is the file handle name. |
|
|
|
The name of the current report output header formatThe default value is the file handle name with the suffix “_TOP “. |
|