4

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
brian d foy
  • 129,424
  • 31
  • 207
  • 592

2 Answers2

5

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.

Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
4

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};
Elizabeth Mattijsen
  • 25,654
  • 3
  • 75
  • 105
Brad Gilbert
  • 33,846
  • 11
  • 78
  • 129