设为首页 收藏本站
查看: 661|回复: 0

[经验分享] Perl Special Variables

[复制链接]

尚未签到

发表于 2015-12-26 17:01:57 | 显示全部楼层 |阅读模式
Some variables have a predefined and special meaning in Perl. They are the variables that use punctuation characters after the usual variable indicator ($, @, or %), such as $_. The explicit, long-form names shown are the variables' equivalents when you use the English module by including "use English;" at the top of your program.



4.4.1 Global Special Variables

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 dies 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>.




4.4.2 Global Special Arrays and Hashes

@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.




4.4.3 Global Special Filehandles

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.




4.4.4 Global Special Constants

__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.




4.4.5 Regular Expression Special Variables

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);
  




4.4.6 Filehandle Special Variables

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.

  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-156725-1-1.html 上篇帖子: Perl opendir()函数 下篇帖子: Use Perl to check Purify result automatically
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表