Variables & sigils

The topic $_

Full

The 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
30

The 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
c

Set 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
5

Notes #