Topic variable
As in Perl, in Raku the topic variable is $_. In addition to a direct assigment, it can be set with the 'given' or 'with' keywords, or by some iteration operator ('for', 'map' etc). A method can be called from it with an implicit call:
Output:
Note: that the $_ inside the 'map' block is different than the $_ set in the 'for' block. The scope of the $_
variable is always lexical in Raku, though a function can access topics in other scopes by specifying the scope: CALLER::<$_>
from within a subroutine, OUTER::<$_>
for the next enclosing block, UNIT::<$_>
for the outermost block in a compilation unit, etc. The scope modifiers may be stacked: CALLER::OUTER::<$_>
would be the topic variable of the next outer block of the current subroutines caller block.
The scope modifiers can only flow outward. There is no way to access the topic of a block enclosed within the current scope or in a separate scope 'branch'.
Also note: Any variable in other scopes can be accessed this way. This is just pointing out that the default (topic) variable also has this property.
Last updated