Subroutines & signatures

Blocks & placeholder parameters

Full

Anonymous code — pointy blocks, the implicit $_, and $^a placeholder parameters.

A block { … } is an anonymous piece of code — a Callable you can store and invoke. It gets its parameters in one of three ways: a pointy signature, the topic $_, or placeholder variables.

Pointy blocks #

-> $a, $b { … } is a block with an explicit signature, just like a sub's.

my $add = -> $a, $b { $a + $b };
say $add(3, 4);
Output
7

The topic — $_ #

A bare block with no signature receives a single argument as the topic $_.

my $sq = { $_ ** 2 };
say $sq(5);
Output
25

Placeholder parameters — $^a #

Inside a signature-less block, $^a-style variables auto-declare parameters. They bind in alphabetical order of their names, regardless of the order they appear.

my $f = { $^a + $^b };
say $f(3, 4);
Output
7

Notes #