Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 904
brian d foy Avatar asked Jan 04 '23 16:01

brian d foy


1 Answers

Making $block.loop work, is rather easy and could live in module land:

use MONKEY;
augment class Block {
    method loop($self:) {
        Nil while $self()
    }
}
my $a = { print "a" };
$a.loop  # aaaaaaaaaaaaaaaaaaa (with apologies to Knorkator)

Making loop $block work would be rather more involved, as this would involve changes to the action handling of the Perl 6 grammar.

like image 113
Elizabeth Mattijsen Avatar answered Jan 06 '23 05:01

Elizabeth Mattijsen