[Melbourne-pm] Perl switch statements

Toby Corkindale toby.corkindale at strategicdata.com.au
Sun Oct 21 22:39:49 PDT 2012


On 22/10/12 16:36, Toby Corkindale wrote:
> Something bugs me about Perl's switch statements.
>
> If you put a given(){} block at the end of a function, the function will
> return the matched result. However if you attempt to assign the result
> of given() directly to a variable, it will fail.
>
> Who came up with this and what were they thinking? I suppose there's a
> good reason, but I can't see what it is.
>
> Compare these:
> ---------------------------------------
>
> sub foo {
>    given (shift) {
>      when ('ay') { "yay" }
>      when ('bee') { "hurrah" }
>      default { "what?" }
>    }
> }
> say foo("bee");
> # outputs: hurrah
>
> ---------------------------------------
>
> sub bar {
>    say given (shift) {
>      when ('ay') { "yay" }
>      when ('bee') { "hurrah" }
>      default { "what?" }
> }

Oops.
My copy of the function above is missing the final closing curly brace. 
It still doesn't work with it though, but you get a different error.


More information about the Melbourne-pm mailing list