The most commonly used special variable is $_
, which contains the default input and pattern-searching string. For example, in the following lines:
foreach ('hickory','dickory','doc') {
print;
}
The first time the loop is executed, "hickory" is printed. The second time around, "dickory" is printed, and the third time, "doc" is printed. That's because in each iteration of the loop, the current string is placed in
$_
, and is used by default by
print
. Here are the places where Perl will assume
$_
even if you don't specify it:
-
Various unary functions, including functions like ord
and int
, as well as the all file tests (-f
, -d
) except for -t
, which defaults to STDIN
.
-
Various list functions like print
and unlink
.
-
The pattern-matching operations m//
, s///
, and tr///
when used without an =~
operator.
-
The default iterator variable in a foreach
loop if no other variable is supplied.
-
The implicit iterator variable in the grep
and map
functions.
-
The default place to put an input record when a line-input operation's result is tested by itself as the sole criterion of a while
test (i.e., <filehandle
>). Note that outside of a while
test, this will not happen.
The following is a complete listing of global special variables:
$_
$ARG
The default input and pattern-searching space.
$.
$INPUT_LINE_NUMBER
$NR
The current input line number of the last filehandle that was read. An explicit close on the filehandle resets the line number.
$/
$INPUT_RECORD_SEPARATOR
$RS
The input record separator; newline by default. If set to the null string, it treats blank lines as delimiters.
$,
$OUTPUT_FIELD_SEPARATOR
$OFS
The output field separator for the print
operator.
$\
$OUTPUT_RECORD_SEPARATOR
$ORS
The output record separator for the print
operator.
$
$LIST_SEPARATOR
Like "$,
" except that it applies to list values interpolated into a double-quoted string (or similar interpreted string). Default is a space.
$;
$SUBSCRIPT_SEPARATOR
$SUBSEP
The subscript separator for multidimensional array emulation. Default is "\034"
.
$^L
$FORMAT_FORMFEED
What a format outputs to perform a formfeed. Default is "\f"
.
$:
$FORMAT_LINE_BREAK_CHARACTERS
The current set of characters after which a string may be broken to fill continuation fields (starting with ^
) in a format. Default is "\n""
.
$^A
$ACCUMULATOR
The current value of the write
accumulator for format
lines.
$#
$OFMT
Contains the output format for printed numbers (deprecated).
$?
$CHILD_ERROR
The status returned by the last pipe close, backtick (``
) command, or system
operator.
$!
$OS_ERROR
$ERRNO
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.
$@
$EVAL_ERROR
The Perl syntax error message from the last eval
command.
$$
$PROCESS_ID
$PID
The pid of the Perl process running this script.
$<
$REAL_USER_ID
$UID
The real user ID (uid) of this process.
$>
$EFFECTIVE_USER_ID
$EUID
The effective uid of this process.
$(
$REAL_GROUP_ID
$GID
The real group ID (gid) of this process.
$)
$EFFECTIVE_GROUP_ID
$EGID
The effective gid of this process.
$0
$PROGRAM_NAME
Contains the name of the file containing the Perl script being executed.
$[
The index of the first element in an array and of the first character in a substring. Default is 0.
$]
$PERL_VERSION
Returns the version plus patchlevel divided by 1000.
$^D
$DEBUGGING
The current value of the debugging flags.
$^E
$EXTENDED_OS_ERROR
Extended error message on some platforms.
$^F
$SYSTEM_FD_MAX
The maximum system file descriptor, ordinarily 2.
$^H
Contains internal compiler hints enabled by certain pragmatic modules.
$^I
$INPLACE_EDIT
The current value of the inplace-edit extension. Use undef
to disable inplace editing.
$^M
The contents of $M
can be used as an emergency memory pool in case Perl die
s with an out-of-memory error. Use of $M
requires a special compilation of Perl. See the INSTALL document for more information.
$^O
$OSNAME
Contains the name of the operating system that the current Perl binary was compiled for.
$^P
$PERLDB
The internal flag that the debugger clears so that it doesn't debug itself.
$^T
$BASETIME
The time at which the script began running, in seconds since the epoch.
$^W
$WARNING
The current value of the warning switch, either true or false.
$^X
$EXECUTABLE_NAME
The name that the Perl binary itself was executed as.
$ARGV
Contains the name of the current file when reading from <ARGV>
.
For more information on regular expressions, see Section 4.6, "Regular Expressions" later in this chapter.
$
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.
$&
$MATCH
The string matched by the last successful pattern match.
$`
$PREMATCH
The string preceding whatever was matched by the last successful pattern match.
$'
$POSTMATCH
The string following whatever was matched by the last successful pattern match.
$+
$LAST_PAREN_MATCH
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 = ___FCKpd___1);
Most of these variables only apply when using formats. See Section 4.10, "Formats" later in this chapter.
$|
$OUTPUT_AUTOFLUSH
If set to nonzero, forces an fflush(3)
after every write
or print
on the currently selected output channel.
$%
$FORMAT_PAGE_NUMBER
The current page number of the currently selected output channel.
$=
$FORMAT_LINES_PER_PAGE
The current page length (printable lines) of the currently selected output channel. Default is 60.
$-
$FORMAT_LINES_LEFT
The number of lines left on the page of the currently selected output channel.
$~
$FORMAT_NAME
The name of the current report format for the currently selected output channel. Default is the name of the filehandle.
$^
$FORMAT_TOP_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.
相关推荐
### 特殊变量(Special Variables) Perl有一系列特殊变量,如`$!`、`@ARGV`、`%ENV`等,它们用于存储错误代码、命令行参数、环境变量等系统级别的信息,对于理解和调试Perl程式非常有用。 总之,Perl的基本语法涵盖...
l Actual Perl code is typeset in a special monospace font. You'll see this font used in listings and the Input-Output examples, as well as in code snippets. In the explanations of Perl features, ...
Special Variables Perl定义了一系列特殊的变量,用于控制程序的行为或获取内部状态信息。 - **$ARGV`、`$ARGVOUT**:命令行参数和输出。 - **$!`、`$@**:错误信息。 ### 28. Special Arrays Perl还有一些特殊...
Special Variables Environment Variables H Program Arguments H Current Line H System Error Messages H G Flow Control Logical Operators H G CONTENTS http://docs.rinet.ru:8080/Using_Perl5_in_Web/ ...
Using "copied" and "selected" variables for dynamic macros Use copied and selected text in macros to dramatically increase the power and flexibility of UltraEdit macros Run a macro or script from the ...
集合了 所有的 Unix命令大全 ...telnet 192.168.0.23 自己帐号 sd08077-you0 ftp工具 192.168.0.202 tools-toolss ... 各个 shell 可互相切换 ksh:$ sh:$ csh:guangzhou% bash:bash-3.00$ ... 命令和参数之间必需用空格隔...
Special Shell Variables B-2. TEST Operators: Binary Comparison B-3. TEST Operators: Files B-4. Parameter Substitution and Expansion B-5. String Operations B-6. Miscellaneous Constructs C-1. Basic sed...
Special Shell Variables B-2. TEST Operators: Binary Comparison B-3. TEST Operators: Files B-4. Parameter Substitution and Expansion B-5. String Operations B-6. Miscellaneous Constructs C-1. Basic sed...