class Block is Code { }

A Block is a code object meant for small-scale code reuse. A block is created syntactically by a list of statements enclosed in curly braces. The literal for creating an empty block is {;}.

Without an explicit signature or placeholder arguments, a block has $_ as a positional argument, which defaults to the outer scope's $_. Thus it will inherit the topic if there is any.

my $block = { uc $_; };
say $block.^name;           # OUTPUT: «Block␤»
say $block('hello');        # OUTPUT: «HELLO␤»
say {;}.signature;          # OUTPUT: «(;; $_? is raw = OUTER::<$_>)␤»

A block can have a Signature between -> or <-> and the block:

my $add = -> $a, $b = 2 { $a + $b };
say $add(40);               # OUTPUT: «42␤»

If the signature is introduced with <->, then the parameters are marked as rw by default:

my $swap = <-> $a, $b { ($a, $b) = ($b, $a) };
my ($a, $b) = (2, 4);
$swap($a, $b);
say $a;                     # OUTPUT: «4␤»

Blocks that aren't of type Routine (which is a subclass of Block) are transparent to return.

sub f() {
    say <a b c>.map: { return 42 };
                   #   ^^^^^^   exits &f, not just the block
}

The last statement is the implicit return value of the block.

say {1}.(); # OUTPUT: «1␤»

Bare blocks are automatically executed in the order they appear:

say 1;                # OUTPUT: «1␤»
{
    say 2;            # OUTPUT: «2␤»; executed directly, not a Block object
}
say 3;                # OUTPUT: «3␤»

Typegraph§

Type relations for Block
raku-type-graph Block Block Code Code Block->Code Mu Mu Any Any Any->Mu Callable Callable Code->Any Code->Callable Routine Routine Routine->Block Submethod Submethod Submethod->Routine Method Method Method->Routine Sub Sub Sub->Routine Macro Macro Macro->Routine Regex Regex Regex->Method

Expand chart above