On Wed, May 04, 2005 at 11:00:31PM -0600, Luke Palmer wrote:
: What should the output of this be:
: 
:     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

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

Reply via email to