rakucontrol-structure

Can you loop a Perl 6 block that's in a variable?


I keep wanting to do something like this:

my $block := {
    state $n = 0;
    say $n++;
    last if $n > 3;
    };

loop $block;

Or even:

$block.loop;

I'm not expecting that this is possible but it would sure be cool if it was.

How would I find out where a particular routine comes from?

$ perl6
To exit type 'exit' or '^D'
> &loop.^name
===SORRY!=== Error while compiling:
Undeclared routine:
    loop used at line 1

Solution

  • Using what is already in PerlĀ 6, you can use Seq.from-loop in sink context.
    (Note that the REPL doesn't put the last statement on a line into sink context)

    my $block := {
        state $n = 0;
        say $n++;
        last if $n > 3;
    }
    
    Seq.from-loop: $block;
    
    Seq.from-loop: {say $++}, {$++ <= 3};