[Melbourne-pm] Perl switch statements

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


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?" }
}
bar("bee");
# outputs:


More information about the Melbourne-pm mailing list