[Melbourne-pm] Perl switch statements

Michael G Schwern schwern at pobox.com
Mon Oct 22 16:29:02 PDT 2012


On 2012.10.21 10:51 PM, Mathew Robertson wrote:
> What is also surprising is this:
> 
> sub moo {
>   given (shift) {
>     when ('ay') { "yay" }
>     when ('bee') { "hurrah" }
>     default { "what?" }
>   };
>   say $_;
> }
> moo("bee");
> 
> 
> I thought the when() implemented the test against $_... 

It does.  $_ is local to the given block, same as a foreach loop.


> or even:
> 
> sub moo {
>   given (shift) {
>     when ('ay') { "yay" }
>     when ('bee') { $_ = "hurrah" }
>     default { "what?" }
>   };
>   say $_;
> }
> moo("bee");

Same deal.  $_ can't escape the given block.  If it did, nested givens would
clobber each other.


-- 
There will be snacks.


More information about the Melbourne-pm mailing list