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:

##$\Output record delimiter$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 a space . ##$LIST_SEPARATOR##$;The separator used when simulating multi-dimensional arrays is "\034. ".$SUBSCRIPT_SEPARATOR$^LThe paper feed sent to the output channel. The default is "\f".$FORMAT_FORMFEEDThe 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$^ AVariable used to save formatted data before printing$ACCUMULATOR$# Default number output format when printing numbers (deprecated).##$!The numerical value of this variable is the value of errno, and the string value is the corresponding system error string$OS_ERROR or $ERRNO$@The error message of command eval. If it is empty, it means that the last eval command was executed successfully. $EVAL_ERROR$$The process number running the current Perl script$PROCESS_ID or $PID$<The actual user ID of the current process$REAL_USER_ID or $UID$>Effective user ID of the current process$EFFECTIVE_USER_ID or $EUID$(The actual group user ID of the current process$REAL_GROUP_ID or $GID$)The effective group user ID of the current process$EFFECTIVE_GROUP_ID or $EGID$0 Contains the file name of the script being executed $PROGRAM_NAME$[Array of array The subscript of an element, which defaults to 0. ##$]$PERL_VERSION$^D$DEBUGGING$^E$EXTENDED_OS_ERROR$^ F$SYSTEM_FD_MAX##$^HCompiled by Syntax check status of editor activation$^IValue of the built-in control editor$INPLACE_EDIT$^MThe size of the backup memory pool## $^PInternal variable specifying the current debug value$PERLDB$^TStarting from the new century, the time the script starts running in seconds$BASETIME##$^W$WARNING$^X$EXECUTABLE_NAME$ARGV##Global array special variables
$_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
##$:
$OFMT
$?Return the status of the previous external command
$CHILD_ERROR

Perl version number
Value of debugging flag
Operating system extended error messages in non-UNIX environments
Maximum file bundle descriptor value

$^OOperating system name
$OSNAME
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

@ARGVList of command line parameters passed to the script@INCList of directories to be searched when importing the module@FCommand line array inputGlobal hash special variable

%INC%ENV%SIG

Global special file handle

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

Logical end of script , ignore the following text. Current file nameCurrent line numberCurrent package name, the default package name is main.
##__END__

__FILE__

__LINE__

__PACKAGE__

Regular expression special variables

contains The nth substring of the last pattern matchThe string of the previous successful pattern matchThe content before the last successfully matched substring##$'$POSTMATCH##$+
/Version: (.*)|Revision: (.*)/ && ($rev = $+);
$LAST_PAREN_MATCH

File handle special variable

$n

$&

$MATCH

$`

$PREMATCH

The previous match was successful Content after the substring

The last bracket matching the previous regular expression search pattern. For example:

The remainder of the current page Number of linesCurrent report The name of the output format. The default value is the file handle name. ##$^$FORMAT_TOP_NAME

$|

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

##$-

$FORMAT_LINES_LEFT

$~

$FORMAT_NAME

Current report output table The name of the header format. The default value is the file handle name with the suffix "_TOP".