control flow once

Documentation for control flow once assembled from the following types:

language documentation Control Flow

From Control Flow

(Control Flow) control flow once

A block prefix with once will be executed exactly once, even if placed inside a loop or a recursive routine.

my $guard = 3;
loop {
    last if $guard-- <= 0;
    once { put 'once' };
    print 'many'
} # OUTPUT: «once␤manymanymany» 

This works per "clone" of the containing code object, so:

({ once 42.say } xx 3).map: {$_(), $_()}# says 42 thrice 

Note that this is not a thread-safe construct when the same clone of the same block is run by multiple threads. Also remember that methods only have one clone per class, not per object.