Variables & sigils
The topic $_
FullThe implicit variable that blocks, loops, and method calls default to.
$_ is the topic — the "it" of the current block. Many constructs set it implicitly, and a leading-dot method call (.method) or a bare given/when operates on it, so you rarely have to name it.
Set by a for loop #
Without a pointy signature, for sets $_ to each element.
for 1..3 { say $_ * 10 }
Output
10
20
30The dot shorthand #
.method is $_.method, so you can call methods on the topic with no variable at all.
for <a b c> { .say }
Output
a
b
cSet by given #
given topicalises its argument for the block, which pairs with when and the dot shorthand.
given "hello" { say .uc; say .chars }
Output
HELLO
5Notes #
$_is an ordinary variable — you can name it, assign it, or take it as a block parameter with-> $_ { … }.map,grep, andsortblocks with no signature receive their argument as$_, which is whymap { .uc }works.- The topic is dynamically scoped by the block that sets it; nesting a
forinside agivenrebinds$_for the inner block.