📜  Perl-特殊变量

📅  最后修改于: 2020-10-16 05:34:10             🧑  作者: Mango


Perl中有一些具有预定义和特殊含义的变量。他们是通常的变量指标($,@或%)后使用字符,如$ _(下文解释)的变量。

大多数特殊变量的名称都像英文一样长,例如,Operating System Error变量$!。可以写为$ OS_ERROR。但是,如果您要使用英语(如名称),那么您将不得不使用英语一行在程序文件的顶部。这将引导解释器理解变量的确切含义。

最常用的特殊变量是$ _,它包含默认输入和模式搜索字符串。例如,在以下几行中-

#!/usr/bin/perl

foreach ('hickory','dickory','doc') {
   print $_;
   print "\n";
}

执行后,将产生以下结果-

hickory
dickory
doc

再次,让我们检查相同的示例,而无需显式使用$ _变量-

#!/usr/bin/perl

foreach ('hickory','dickory','doc') {
   print;
   print "\n";
}

当执行时,这还将产生以下结果-

hickory
dickory
doc

第一次执行循环时,将打印“山核桃”。第二次打印“目录”,第三次打印“ doc”。这是因为在循环的每次迭代中,当前字符串都放在$ _中,并且在默认情况下由print使用。这是Perl即使不指定$ _也会假设$ _的地方-

  • 各种一元函数,包括ord和int之类的函数,以及除-t外的所有文件测试(-f,-d),默认情况下为STDIN。

  • 各种列表功能,例如打印和取消链接。

  • 在不使用=〜运算符的情况下使用模式匹配操作m //,s ///和tr ///。

  • 如果未提供其他变量,则为foreach循环中的默认迭代器变量。

  • grep和map函数中的隐式迭代器变量。

  • 单独测试行输入操作的结果时,放置输入记录的默认位置是while测试(即)的唯一标准。请注意,在一段时间测试之外,不会发生这种情况。

特殊变量类型

根据特殊变量的用法和性质,我们可以将其分类为以下几类:

  • 全局标量特殊变量。
  • 全局数组特殊变量。
  • 全局哈希特殊变量。
  • 全球特殊文件句柄。
  • 全球特殊常数。
  • 正则表达式特殊变量。
  • Filehandle特殊变量。

全局标量特殊变量

这是所有标量特殊变量的列表。我们已经列出了相应的英语名称和符号名称。

$_ The default input and pattern-searching space.
$ARG
$. The current input line number of the last filehandle that was read. An explicit close on the filehandle resets the line number.
$NR
$/ The input record separator; newline by default. If set to the null string, it treats blank lines as delimiters.
$RS
$, The output field separator for the print operator.
$OFS
$\ The output record separator for the print operator.
$ORS
$” Like “$,” except that it applies to list values interpolated into a double-quoted string (or similar interpreted string). Default is a space.
$LIST_SEPARATOR
$; The subscript separator for multidimensional array emulation. Default is “\034”.
$SUBSCRIPT_SEPARATOR
$^L What a format outputs to perform a formfeed. 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 current value of the write accumulator for format lines.
$ACCUMULATOR
$# Contains the output format for printed numbers (deprecated).
$OFMT
$? The status returned by the last pipe close, backtick (“) command, or system operator.
$CHILD_ERROR
$! If used in a numeric context, yields the current value of the errno variable, identifying the last system call error. If used in a string context, yields the corresponding system error string.
$OS_ERROR or $ERRNO
$@ The Perl syntax error message from the last eval command.
$EVAL_ERROR
$$ The pid of the Perl process running this script.
$PROCESS_ID or $PID
$< The real user ID (uid) of this process.
$REAL_USER_ID or $UID
$> The effective user ID of this process.
$EFFECTIVE_USER_ID or $EUID
$( The real group ID (gid) of this process.
$REAL_GROUP_ID or $GID
$) The effective gid of this process.
$EFFECTIVE_GROUP_ID or $EGID
$0 Contains the name of the file containing the Perl script being executed.
$PROGRAM_NAME
$[ The index of the first element in an array and of the first character in a substring. Default is 0.
$] Returns the version plus patchlevel divided by 1000.
$PERL_VERSION
$^D The current value of the debugging flags.
$DEBUGGING
$^E Extended error message on some platforms.
$EXTENDED_OS_ERROR
$^F The maximum system file descriptor, ordinarily 2.
$SYSTEM_FD_MAX
$^H Contains internal compiler hints enabled by certain pragmatic modules.
$^I The current value of the inplace-edit extension. Use undef to disable inplace editing.
$INPLACE_EDIT
$^M The contents of $M can be used as an emergency memory pool in case Perl dies with an out-of-memory error. Use of $M requires a special compilation of Perl. See the INSTALL document for more information.
$^O Contains the name of the operating system that the current Perl binary was compiled for.
$OSNAME
$^P The internal flag that the debugger clears so that it doesn’t debug itself.
$PERLDB
$^T The time at which the script began running, in seconds since the epoch.
$BASETIME
$^W The current value of the warning switch, either true or false.
$WARNING
$^X The name that the Perl binary itself was executed as.
$EXECUTABLE_NAME
$ARGV Contains the name of the current file when reading from .

全局数组特殊变量

@ARGV The array containing the command-line arguments intended for the script.
@INC The array containing the list of places to look for Perl scripts to be evaluated by the do, require, or use constructs.
@F The array into which the input lines are split when the -a command-line switch is given.

全局哈希特殊变量

%INC The hash containing entries for the filename of each file that has been included via do or require.
%ENV The hash containing your current environment.
%SIG The hash used to set signal handlers for various signals.

全局特殊文件句柄

ARGV The special filehandle that iterates over command line filenames in @ARGV. Usually written as the null filehandle in <>.
STDERR The special filehandle for standard error in any package.
STDIN The special filehandle for standard input in any package.
STDOUT The special filehandle for standard output in any package.
DATA The special filehandle that refers to anything following the __END__ token in the file containing the script. Or, the special filehandle for anything following the __DATA__ token in a required file, as long as you’re reading data in the same package __DATA__ was found in.
_ (underscore) The special filehandle used to cache the information from the last stat, lstat, or file test operator.

全球特殊常数

__END__ Indicates the logical end of your program. Any following text is ignored, but may be read via the DATA filehandle.
__FILE__ Represents the filename at the point in your program where it’s used. Not interpolated into strings.
__LINE__ Represents the current line number. Not interpolated into strings.
__PACKAGE__ Represents the current package name at compile time, or undefined if there is no current package. Not interpolated into strings.

正则表达式特殊变量

$digit Contains the text matched by the corresponding set of parentheses in the last pattern matched. For example, $1 matches whatever was contained in the first set of parentheses in the previous regular expression.
$& The string matched by the last successful pattern match.
$MATCH
$` The string preceding whatever was matched by the last successful pattern match.
$PREMATCH
$’ The string following whatever was matched by the last successful pattern match.
$POSTMATCH
$+ The last bracket matched by the last search pattern. This is useful if you don’t know which of a set of alternative patterns was matched. For example : /Version: (.*)|Revision: (.*)/ && ($rev = $+);
$LAST_PAREN_MATCH

Filehandle特殊变量

$| If set to nonzero, forces an fflush(3) after every write or print on the currently selected output channel.
$OUTPUT_AUTOFLUSH
$% The current page number of the currently selected output channel.
$FORMAT_PAGE_NUMBER
$= The current page length (printable lines) of the currently selected output channel. Default is 60.
$FORMAT_LINES_PER_PAGE
$- The number of lines left on the page of the currently selected output channel.
$FORMAT_LINES_LEFT
$~ The name of the current report format for the currently selected output channel. Default is the name of the filehandle.
$FORMAT_NAME
$^ The name of the current top-of-page format for the currently selected output channel. Default is the name of the filehandle with _TOP appended.
$FORMAT_TOP_NAME