I think you could do that without the macro-magic with simple subs likemacro statement_control:<my_while>($expr, &whileblock) { while $expr &whileblock(); # do we need while $expr do &whileblock(); ? } my $i = 0; my_while {$i < 10} { say "i: $i"; my $j = 0; my_while {$j < 10} { say "j: $j"; $j++; } $i++; }
This is because (from S4):sub my_while(Code &expr, Code &whileblock) { while &expr() &whileblock(); }
Every block is a closure. (That is, in the abstract, they're all anonymous subroutines that take a snapshot of their lexical scope.) How any block is invoked and how its results are used is a matter of context, but closures all work the same on the inside.
Digging around the Perl 6 specs, I begin to see that perhaps S4 holds some of the nicest things in the design of Perl 6. And, quite frankly, the more I read the more I feel how much I miss a working Perl 6 compiler...
Update: hey, we can, with a slightly more convoluted syntax, do this with Perl 5 too:
Then, what's the great deal?use strict; use warnings; sub my_while(&&){ my($expr, $whileblock) = @_; while(&$expr()){ &$whileblock(); } } my $i = 0; my_while sub {$i < 10}, sub { print "i: $i\n"; my $j = 0; my_while sub {$j < 10}, sub { print "j: $j\n"; $j++; }; $i++; };
rg0now
In reply to Re: Perl 6 and Ruby on Rails
by rg0now
in thread Perl 6 and Ruby on Rails
by mattr
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |