<div><br></div><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="im">
> What is also surprising is this:<br>
><br>
> sub moo {<br>
>   given (shift) {<br>
>     when ('ay') { "yay" }<br>
>     when ('bee') { "hurrah" }<br>
>     default { "what?" }<br>
>   };<br>
>   say $_;<br>
> }<br>
> moo("bee");<br>
><br>
><br>
> I thought the when() implemented the test against $_...<br>
<br>
</div>It does.  $_ is local to the given block, same as a foreach loop.<br></blockquote><div><br></div><div>I always thought that "unless $_ is explicitly localised"... would manipulate the global $_</div><div>

<br></div><div><div>foreach (1..2) {</div><div>  foreach ('a'..'c') {</div><div>    print $_." ";</div><div>    last;</div><div>  }</div><div>  print $_." ";</div><div>}</div><div>print $/;</div>

<div><br></div>ie: the output is  "a 1 a 2"... on v5.14 and v5.8.8</div><div><br></div><div>... I could have sworn that I have used that idiom in the past, expecting to get "a a a a"...</div><div><br>
</div>
<div>but there you go.</div><div><br></div><div>To clarify, I would have expected $x to not be lexical... I am deliberately reusing $x in the child scope assignment, without localising $x (via 'my' or 'local'):</div>

<div><br></div><div><div>my $x = 'foo';</div><div>foreach $x (1..2) {</div><div>  foreach $x ('a'..'c') {</div><div>    print $x." ";</div><div>  }</div><div>  print $x." ";</div>

<div>}</div><div>print $x.$/;</div></div><div><br></div><div>gives: a b c 1 a b c 2 foo</div><div>when I told the code to generate: a b c c a b c c c</div><div><br></div><div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">


<div class="im"><br>
<br>
> or even:<br>
><br>
> sub moo {<br>
>   given (shift) {<br>
>     when ('ay') { "yay" }<br>
>     when ('bee') { $_ = "hurrah" }<br>
>     default { "what?" }<br>
>   };<br>
>   say $_;<br>
> }<br>
> moo("bee");<br>
<br>
</div>Same deal.  $_ can't escape the given block.  If it did, nested givens would<br>
clobber each other.<br></blockquote><div><br></div><div>I'm going to say either a) no it wouldn't, or b) that is expected...... depending on the use case.</div><div><br></div><div>ie: either you are in block scope so other 'when's arn't executed, or you are explicitly manipulating $_ so that you can deliberately could cause manipulation of the outer scope.</div>

<div><br></div><div><br></div><div><br></div><div>In any case, the point is moot as $_ or $x are localised to the scope - and we (well 'I') learnt something new.</div><div><br></div><div>cheers,</div><div>Mathew</div>

<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"></div></blockquote></div>