in reply to Perl 6 and Ruby on Rails
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Perl 6 and Ruby on Rails
by duff (Parson) on Apr 05, 2005 at 18:09 UTC | |
by rg0now (Chaplain) on Apr 05, 2005 at 20:25 UTC | |
|
Re^2: Perl 6 and Ruby on Rails
by dragonchild (Archbishop) on Apr 05, 2005 at 16:23 UTC | |
|
Re^2: Perl 6 and Ruby on Rails
by jdporter (Paladin) on Apr 05, 2005 at 19:02 UTC | |
|
Re^2: Perl 6 and Ruby on Rails
by mattr (Curate) on Apr 05, 2005 at 17:34 UTC |