On Thu, Feb 14, 2008 at 04:16:02PM -0700, Thom Boyer wrote:
> S02 says "A bare closure also interpolates in double-quotish context."
>
> I presume that there are no restrictions on the code inside that closure,
> but all the examples I've seen have nothing but expressions inside the
> closure (though some examples, admittedly, do invoke subs and/or methods).
>
>
> Question 1: Does
>
> my $s = ''
> say "Fire in the hole!{
> for reverse 1 .. 3 { $s = qq[$s $_]; }
> $s
> } BOOM!";
>
> work? I.e., does it say this?
>
> Fire in the hole! 3 2 1 BOOM!
>
> I'm not arguing that embedding that much code in a string is good style.
> I'm just asking if it's forbidden.
It's perfectly legal. All closures in Perl 6 allow multiple statements.
> Question 2: Does C<for> return the value of its last statement? In other
> words, does this have the same effect as the previous example?
>
> my $s = '';
> say "Fire in the hole!{for reverse 1..3 { $s = qq[$s $_] }} BOOM!";
All complete iterations return their final value as a list. Therefore
that code will print
Fire in the hole! 3 3 2 3 2 1 BOOM!
which is likely to confuse your hired help. What you want is just
say "Fire in the hole!{for reverse 1..3 { $_ }} BOOM!";
This is described under Loop Statements in S04.
> Question 3: Do quotes inside a closure inside a string get parsed exactly
> as if they were in code, or do they screw up the scanning of the outermost
> string? If the closure contains "real" code, then I should be able to
> replace qq[$s $_] with "$s $_" in the example. Does this work?
>
> my $s = '';
> say "Fire in the hole!{for reverse 1..3 { $s = "$s $_" }} BOOM!";
> # does this quote end the scan of string? -----^
No, there's no problem with that. This is Perl 6, which is full of
wonderfulness, not Perl 5, which was written by a person of minimal clue. :)
That's part of what S02 means right at the top where it's talking
about a one-pass parser. There's no lookahead to find the end of a
construct. You just come to it when you come to it, and the parser
has to be smart enough to know which terminators mean what in each
context.
Larry