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 $_
打印不指定变量时就答应 $_
转载于:https://blog.51cto.com/myunix/1160205