您的位置:首页 > 其它

PERL特殊变量

2009-11-02 10:37 239 查看
Perl是个有个很多特殊变量的强大语言,这些特殊变量不需要程序员干涉就能够提供信息。

$.: 从当前输入文件中的去的行数。

$$: 当前进程ID

$^O: 当前操作系统

其他特殊变量反映了一些操作如何被执行。

$| 控制输出缓冲

$_ 默认输入和模式匹配空间。
如果从while循环的顶部读入一个文件句柄,或者运行一个foreach循环而且不知名一个循环变量,$_是为你准备的。

while ($line = <FH>) {

if ($line =~ /Perl/) {

print FHO $line;

}

print uc $line;

}


Shortens to:

while (<FH>) {

/Perl/ and

print FHO ;

print uc;

}


@_ 函数的输入参数


So if you write a sub, you refer to the first parameter in it as $_[0]
, the second parameter as $_[1]
and so on. And you can refer to $#_
as the index number of the last parameter:

sub demo {

print "Called with ",$#_+1," params/n";

print "First param was $_[0]/n";


Note that the English
module adds in the ability to refer
to the special variables by other longer, but easier to remember, names
such as @ARG for @_ and $PID for $$. But
use English;

can have a detrimental performance effect if you're matching regular expressions against long incoming strings.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: