When scoping

1 view
Skip to first unread message

Luke Palmer

unread,
May 5, 2005, 1:00:31 AM5/5/05
to perl6-l...@perl.org, perl6-c...@perl.org
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

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

Larry Wall

unread,
May 5, 2005, 1:31:04 AM5/5/05
to perl6-l...@perl.org, perl6-c...@perl.org
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

Luke Palmer

unread,
May 5, 2005, 2:03:14 AM5/5/05
to perl6-l...@perl.org, perl6-c...@perl.org
On 5/4/05, Larry Wall <la...@wall.org> wrote:
> 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.

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

Larry Wall

unread,
May 5, 2005, 2:19:45 AM5/5/05
to perl6-l...@perl.org, perl6-c...@perl.org
On Wed, May 04, 2005 at 11:00:46PM -0700, David Wheeler wrote:
: On May 4, 2005, at 22:31 , Larry Wall wrote:
:
: > given "hello" {

: > when /hello/ {
: > say "One";
: > if /hello/ { say "Two"; }
: > if /hello/ { say "Three"; }
: > continue;
: > }
: > say "Four";
: > }
:
: Is there no more
:
: say "Two" if /hello/;
:
: ?

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

Reply all
Reply to author
Forward
0 new messages