首页 > 代码库 > perl's Favorite Default: $_
perl's Favorite Default: $_
If you omit the control varibele from the beiginning of foreach loop, Perl uses its favrorite default variable $_. This is (mostly) just like any other scalar variable, except for its unusual name. For example:
foreach (1..10) { #use $_ by default print "I can count to $_!\n"; }
Althongh this isn‘t Perl‘s only default by a long shot, it‘s Perl‘s most common default. You will see many other cases in which Perl will automatically use $_ when you don‘t tell it to use some other variable or value, thereby saving the programmer from the heavy labor of having to thinking up and type a new variable name. So as not to keep you in suspense, one of those case is print, which will print $_ if given no other argument:
$_ = "yaboo dabba doo";print; #print $_ by default
From p54 of the book named Learning Perl
loop 圈,环,回来。在程序语言里,应该译为“循环”,比如for循环。
scalar 数量,标量。在perl里,就是指的这种数据类型,singular,区别于列表(list)。
not by a long shot 绝不、远没有
in suspense 悬而未决,处于焦灼等待状态,心神不宁;吊胃口
perl's Favorite Default: $_