perl中@_,$_和$1,$2,...及其类似变量的含义
来源:互联网 发布:游戏美工 展uv 软件 编辑:程序博客网 时间:2024/05/22 12:14
Perl's a great language for special variables - variables that are set up without the programmer having to intervene and providing information ranging from the number of lines read from the current input file ($.) through the current process ID ($$) and the operating system ($^O). Other special variables effect how certain operations are performed ($| controlling output buffering / flushing, for example), or are fundamental in the operation of certain facilities - no more so than $_ and @_.
-------------------------------------------
1.@_含义
1)是perl中默认的数组变量
比如说你想移除数组中的一个元素赋值给一个变$value
方法1:你可以定义某个数组如@abcd
my $value=shift @abcd;
方法2:你没有定义任何数组
my $value=shift @_; 和上例等效
这里perl会隐式的选择@_
2)是sub子函数中的默认参数列表.
例如:
sub funct($$) {
($param1, $param2) = @_;
#Statement
}
再例如,有下面一段代码:
my $max_number = &max(1,2);
print "1 and 2 ,the max number is $max_number\n";
sub max{
my ($num1,$num2) = @_[0,1]; ## 取出参数列表中的元素。
........此处省略求max运算
}
在子函数中直接shift; 就可以从@_的前端弹出一个元素.
shift;
等于
shift @_;
-------------------------
@_ is the list of incoming parameters to a sub. 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
2.$_含义
1)$_为默认列表变量。在一个命令没有任何参数的时候,表示它从默认变量里读取。
例如:
print;
等于
print $_;
2)默认模式匹配空间(pattern matching space)
s/.../.../;
等于
$_ =~ s/.../.../;
---------------------------
Then any regular expression matches, chops (and lcs and many more) without a parameter, and evenprints assume you want to work on $_. Thus:
while ($line = <FH>) {
if ($line =~ /Perl/) {
print FHO $line;
}
print uc $line;
}
Shortens to:
while (<FH>) {
/Perl/ and
print FHO ;
print uc;
}
3.$1,$2,...等含义
以数字为名的变量保存的是上一次匹配操作(/pattern/)中,第n个小括号中的原符号所匹配内容。
$1就是第一对小括号中的原符号所对应的匹配内容。
$2就是第二对小括号中的原符号所对应的匹配内容。
内插功能:
$str = "aaa4zzz7bbb";$str =~ /(\d)z{3}(\d)/;
print "$1\t$2\n";
输出结果是:4 7
- perl中@_,$_和$1,$2,...及其类似变量的含义
- perl基础:perl中@_,和1,$2,...及其类似变量的含义
- perl基础:perl中@_, 和 1,$2,...及其类似变量的含义
- 2010-02-02 10:01 perl 中 $1 $2 类似变量的含义
- perl的变量@_
- shell变量_$#,$@,$0,$1,$2的含义解释
- Perl里的特殊变量$_
- perl中@_与$_
- perl @_ $_ 的意思
- Perl 中的特殊变量 $&, $`,$' ,@_
- perl 中$1 $2代表的含义
- Perl中$1的含义
- 3 Perl - 最常用的默认变量:$_
- Perl语言中的 $_ 和 @_
- IOS 中self. 和_访问变量的区别
- perl编程中的$_[0],$_[1]
- c++ primer 学习笔记2_变量及其初始化
- 12_变量_成员变量和局部变量_常量_命名规范
- ubuntu 12.04系统黑屏
- 数据可视化工具搜集总结
- 比较两个浮点数是否相等
- iar Error[Pe007]: unrecognized token
- 2013阿里技术嘉年华:Android设备体验优化
- perl中@_,$_和$1,$2,...及其类似变量的含义
- C/C++中的void和void*
- 关于心跳机制,SO_KEEPALIVE套接口选项的一些些
- 更新Android SDK之后Eclipse提示SDK版本过低的一个简易解决办法
- 常用基础Vim命令
- 图形设备接口(GDI)与设备描述表(DC)
- 编程珠玑:第7章(初略估算)的阅读体会
- java Web 利器之Chrome F12
- Javascript极速快感