Here is my strike at "Passing code blocks around as macros":
macro 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++; }
I think you could do that without the macro-magic with simple subs like
sub my_while(Code &expr, Code &whileblock) { while &expr() &whileblock(); }
This is because (from S4):
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:

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++; };
Then, what's the great deal?

rg0now


In reply to Re: Perl 6 and Ruby on Rails by rg0now
in thread Perl 6 and Ruby on Rails by mattr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.