Perl special variable


Release date:2023-10-20 Update date:2023-10-24 Editor:admin View counts:405

Label:

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() and int() such functions and all file test operations except “- t” (”- f”, “- d”), “- t” default operation STDIN.

  • Various list functions, such as print() and unlink() .

  • 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() and map() 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.

$ARG

$.

The current line number of the last read file handle

$NR

$/

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.

$RS

$,

Output domain delimiter

$OFS

$\

Output record delimiter

$ORS

$"

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.

$LIST_SEPARATOR

$;

The delimiter used when simulating multidimensional arrays. The default is “034”.

$SUBSCRIPT_SEPARATOR

$^L

The feed feed character sent to the output channel. The 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

Variables used to save formatted data before printing

$ACCUMULATOR

$#

The default digital output format (obsolete) when printing numbers.

$OFMT

$?

Returns the status of the previous external command

$CHILD_ERROR

$!

The numeric value of this variable is the value of errno, and the string value is the corresponding system error string

$OS_ERROR or $ERRNO

$@

Error message for command eval. If empty, it indicates successful execution of the previous eval command

$EVAL_ERROR

$$

The process number that runs the current Perl script

$PROCESS_ID or $PID

$<

Actual user number of the current process

$REAL_USER_ID or $UID

$>

The valid user number of the current process

$EFFECTIVE_USER_ID or $EUID

$(

The actual group user number of the current process

$REAL_GROUP_ID or $GID

$)

The valid group user number of the current process

$EFFECTIVE_GROUP_ID or $EGID

, 0

The file name that contains the script being executed

$PROGRAM_NAME

$[

The subscript of the first element of the array. The default is 0.

$]

The version number of Perl

$PERL_VERSION

$^D

The value of the debugging flag

$DEBUGGING

$^E

Operating system extension error message in non-UNIX environment

$EXTENDED_OS_ERROR

$^F

Maximum file bundle descriptor value

$SYSTEM_FD_MAX

$^H

Syntax check status activated by the compiler

$^I

Value of the built-in control editor

$INPLACE_EDIT

$^M

Size of the spare memory pool

$^O

Operating system name

$OSNAME

$^P

Specifies the internal variable of the current debug value

$PERLDB

$^T

The time to start running in seconds from the beginning of the new century.

$BASETIME

$^W

The current value of the warning switch

$WARNING

$^X

The name of the Perl binary executable code

$EXECUTABLE_NAME

$ARGV

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

%INC

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

%ENV

Contains the current environment variable

%SIG

Signal list and its processing method

Global special file handle

ARGV

Special file handles for traversing 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 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__

_ (underline)

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

$n

Contains the nth substring of the last pattern match

$&

The string of the previous successful pattern match

$MATCH

$ `

The content before the previous successful substring match

$PREMATCH

$'

The content after the previous successful matching substring

$POSTMATCH

$+

The last parenthesis that matches the previous regular expression search format. For example:

$LAST_PAREN_MATCH

/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

$OUTPUT_AUTOFLUSH

$%

Current output page number

$FORMAT_PAGE_NUMBER

$=

The current length per page. The default is 60.

$FORMAT_LINES_PER_PAGE

$-

The number of rows left 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 formatThe default value is the file handle name with the suffix “_TOP “.

$FORMAT_TOP_NAME

Powered by TorCMS (https://github.com/bukun/TorCMS).