变量中§
请参阅主要文档 在上下文中 了解$ 变量
$_ 是主题变量。在每个块中创建一个新的变量。它也是没有显式签名的块的默认参数,因此像 for @array { ... } 和 given $var { ... } 这样的构造通过调用块将变量的值或值绑定到 $_。
for <a b c> # binds $_ to 'a', 'b' and 'c' in turnsay for <a b c>; # same, even though it's not a blockgiven 'a' # binds $_ to 'a'say given 'a'; # same, even though it's not a block
因为 $_ 绑定到迭代的值,所以如果 $_ 绑定到可赋值的东西,你也可以赋值给 $_。
my = ^5; # 0 through 4++ for ; # increment all elements of @numberssay ;# OUTPUT: «1 2 3 4 5»
CATCH 块将 $_ 绑定到捕获到的异常。~~ 智能匹配运算符将 $_ 在右侧表达式中绑定到左侧表达式的值。
可以通过省略变量名来缩短对 $_ 的方法调用
.say; # same as $_.say
m/regex/ 和 /regex/ 正则表达式匹配和 s/regex/subst/ 替换在 $_ 上工作
say "Looking for strings with non-alphabetic characters...";for <ab:c d$e fgh ij*># OUTPUT: «Looking for strings with non-alphabetic characters...# ab:c# d$e# ij*»