Perl Special Variables
Perl 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-named special variables, you need to add **use English;** at the beginning of the program. This allows you to use the descriptive English special variables.
The most commonly used special variable is $_, which contains the default input and pattern matching content. The example is as follows:
## Example
#!/usr/bin/perl foreach('Google','','Taobao'){print$_; print"n"; }
Executing the above program, the output is:
GoogleTutorialTaobao
In the following example, we do not use $_ to output content:
## Example
#!/usr/bin/perl foreach('Google','','Taobao'){print; print"n"; }
Executing the above program, the output is:
GoogleTutorialTaobao
In the example, "Google" is printed first, followed by "", and finally "Taobao".
In iteration loops, the string of the current loop is placed in $_, and then output via print. Additionally, when print does not specify an output variable, it defaults to using $_.
Here 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" defaults to operating on STDIN.
* 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 for "foreach" loops when no other variable is given.
* The implicit iteration variable for grep() and map() functions.
* When "while" has only a single condition, and that condition is a test of the result of an operation, $_ is the default location for storing the input record. This does not happen except in the "while" test condition. (Mnemonic: The underscore can be omitted in specific operations.)
* * *
## Special Variable Types
Based on the usage characteristics of special variables, they can be classified 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 scalar special variables, including both the special character and English form of the variables:
$_ Default input and pattern matching content.
$ARG
$. The current line number of the last file handle read.
$NR
$/ Input record separator, default is the newline character. If set to undef, it reads until the end of the file.
$RS
$, Output field separator
$OFS
$ Output record separator
$ORS
$" This variable is similar to $,, but applies to interpolating arrays and slice values into double-quoted strings (or similar interpolated strings). Default is a space.
$LIST_SEPARATOR
$; The separator used when simulating multi-dimensional arrays. Default is "34".
$SUBSCRIPT_SEPARATOR
$^L The form feed character sent to the output channel. Default is "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 store 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 errno value, and the string value is the corresponding system error string.
$OS_ERROR or $ERRNO
$@ The error message from the eval command. If empty, it indicates the last eval command executed successfully.
$EVAL_ERROR
$$ The process ID of the current Perl script.
$PROCESS_ID or $PID
$ 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 Perl version number.
$PERL_VERSION
$^D The value of the debug flag.
$DEBUGGING
$^E Extended operating system error information in non-UNIX environments.
$EXTENDED_OS_ERROR
$^F The maximum file descriptor number.
$SYSTEM_FD_MAX
$^H Syntax checking status activated by the compiler.
$^I The value of the built-in control editor.
$INPLACE_EDIT
$^M The size of the backup memory pool.
$^O The operating system name.
$OSNAME
$^P An internal variable specifying the current debug value.
$PERLDB
$^T The time in seconds since the script started running, calculated from the new century.
$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 The array input from the command line.
### Global Hash Special Variables
%INC The hash %INC contains all files included via 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 their handling methods.
### Global Special File Handles
ARGV A special file handle that iterates through all filenames in the array variable @ARGV.
STDERR Standard error output handle.
STDIN Standard input handle.
STDOUT Standard output handle.
DATA A special file handle that references anything after the __END__ marker in the file, including script content. Or it references all content after the __DATA__ marker in a file, as long as you are reading data in the same package, __DATA__ 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; text after this is ignored.
__FILE__ The current filename.
__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 matched in the last successful match.
$PREMATCH
$' The content after the substring matched in the last successful match.
$POSTMATCH
$+ The last bracket matched by the last successful regular expression 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
$% The current output page number.
$FORMAT_PAGE_NUMBER
$= The current page length. Default is 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 value is the file handle name.
$FORMAT_NAME
$^ The name of the current report output header format. The default value is the file handle name with the suffix "_TOP".
$FORMAT_TOP_NAME
YouTip