perl的老地方$_

简介:

foreach 省略了控制变量,Perl就使用他的老地方变量$_代替。
[root@Master perl]# less LaoDiFang.pl 
#!/usr/bin/perl
foreach (1..10) {
  print "I can count to $_!\n";
}
[root@Master perl]# perl LaoDiFang.pl 
I can count to 1!
I can count to 2!
I can count to 3!
I can count to 4!
I can count to 5!
I can count to 6!
I can count to 7!
I can count to 8!
I can count to 9!
I can count to 10!
 

[root@Master perl]# less LaoDiFang1.pl 
#!/usr/bin/perl
$_ = "Hello LaoDiFang \$_";  
print;
print "\n";
[root@Master perl]# perl LaoDiFang1.pl 
Hello LaoDiFang $_

打印不指定变量时就答应 $_




本文转自 freeterman 51CTO博客,原文链接:http://blog.51cto.com/myunix/1160205,如需转载请自行联系原作者

相关文章
|
4月前
|
Perl
|
4月前
|
Perl
|
4月前
|
Perl
|
4月前
|
Perl
|
4月前
|
Perl
|
4天前
|
大数据 Perl
探索研究Perl 引用
【9月更文挑战第14天】
45 20
|
3月前
|
Perl 自然语言处理 算法
Perl 教程 之 Perl 子程序(函数) 3
Perl 中的子程序(也称为函数)是用于执行特定任务的代码块。您可以在程序的多个位置调用这些子程序,以提高代码的可重用性和模块性。在 Perl 中,子程序可以接收参数,并可以返回值。 以下是 P
|
4月前
|
存储 移动开发 Perl
|
4月前
|
移动开发 Perl
|
4月前
|
自然语言处理 Perl