From: Allison Randal
> Garrett Goebel wrote:
> > 
> > Why does C<when>'s EXPR pay attention to the topicalizer 
> > regardless of associated variable?
> > 
> > Why introduce the special case?
....
>
> Why? Because it's oh-so dwim. Think about it, if you've just typed a
> 
>       given $x { ...
> or
>       given $x -> $y { ...
> 
> you know for a fact that you're going to want every C<when> to
> compare against the $_ or $y. Why force people to type:
> 
>               when $y =~ /a/ {...}
>               when $y =~ /b/ {...}
>               ...
> 
> when you already know what they mean? And yes, it's the 
> common case. How many times do you think you'll have a
> switch statement and want the case to compare against
> some value external to the switch?

Not just some value external to the switch, but the value in $_.

I now see the DWIM aspect. Thanks BTW.

But how often will people have non- C<when> statements within a C<given>
scope that'll need the special case handling so they can see a different $_
than C<when>?

To adapt your example:

$hi = 'hello';
$x  = 'burt';
for $hi {
  given $x -> $y {
    when /burt/ { print "Go Away" };
    default { print };
  }
}

or without the special case:

$hi = 'hello';
$x  = 'burt';
for $hi -> $y {
  given {
    when /burt/ { print "Go Away" };
    default { print };
  }
}

The second is obviously more explict, but does but requires a few more keys
be typed. I wonder if the C<given>/C<when> special case is really going to
be used regularly enough to justify the loss of clarity, consistency, and
the additional obfuscation potential.

$_ = 'foo';
given 'bar' -> $z {       
  if /foo/ { ... }        # true,  $_ = foo
  when 'bar' {            # true,  $_ = bar
    if /foo/ { ... };     # true,  $_ = foo
  }
  $_ = 'baz';             #        $_ = baz
  when 'bud' {            # false, $_ = bar
    if /baz/ { ... };     # true,  $_ = baz
  }
}


I guess the next question in the context of the following is:

Larry Wall wrote in Apocalypse 4:
> It should be possible to make user-extensible syntax look
> just like built-in syntax. 

How would I create a user-extensible construct that works like given/when?
I'm guessing the answer is: you don't.

Reply via email to