given "hello" {
when /hello/ {
say "One";
when /hello/ { say "Two"; }
when /hello/ { say "Three"; }
continue;
}
say "Four";
}
I think:
One
Two
Three
Four
But pugs thinks:
One
Two
The trouble is that I can't seem to come up with an example to test
whether the nested whens are breaking out of the outer when or the
given. Anyway, I'm more curious what Perl 6 thinks it should do.
Luke
I'm inclined to think Pugs has it right here.
: The trouble is that I can't seem to come up with an example to test
: whether the nested whens are breaking out of the outer when or the
: given. Anyway, I'm more curious what Perl 6 thinks it should do.
Perl 6 thinks :-) that "break" (either implicit or explicit) should
break out of the topicalizer block and ignore non-topicalizer
intermediate blocks, just as "next" and "last" ignore any non-loop
blocks to find the innermost enclosing loop, and "return" ignores
inner blocks to find the true "sub" block. (And the inner "when"
isn't considered a retopicalization.)
To get the other behavior, you have to say one of:
given "hello" {
when /hello/ {
say "One";
when /hello/ { say "Two"; continue; }
when /hello/ { say "Three"; continue; }
continue;
}
say "Four";
}
given "hello" {
when /hello/ {
say "One";
say "Two" when /hello/;
say "Three" when /hello/;
continue;
}
say "Four";
}
given "hello" {
when /hello/ {
say "One";
if /hello/ { say "Two"; }
if /hello/ { say "Three"; }
continue;
}
say "Four";
}
That seems like enough WTDI. (Though arguably, the "continue" version
could be defined to skip Three. But it seems more straightfoward
to define it as leaving the current when block.)
Larry
Not quite. There's also the way that I have it in the pugs IRC bot:
given "hello" {
when /hello/ {
given $_ {
say "One";
when /hello/ { say "Two" }
when /hello/ { say "Three" }
}
}
say "Four";
}
Which I actually like the best. If only it weren't so heavily indented.
Hmmm, maybe I should go implement Acme::Whiven:
given "hello" {
whiven /hello/ {
when /hello/ {...}
...
}
}
Luke
You must have missed the implied "..." at the end of my list of other WTDI.
You can also do any of:
say "Two" if /hello/;
/hello/ && say "Two";
/hello/ and say "Two";
/hello/ ?? say "Two" :: leave;
infix:<and>(/hello/, { say "Two" })
continue unless /hello/; say "Two";
/hello { say "Two" }/;
s/hello/{ say "Two" }$0/;
({}, { say "Two" })[?/hello/]();
and probably a few more I can't think of off the top of my head.
Larry