[Melbourne-pm] Perl switch statements
Mathew Robertson
mathew.blair.robertson at gmail.com
Sun Oct 21 22:51:48 PDT 2012
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 $_...
or even:
sub moo {
given (shift) {
when ('ay') { "yay" }
when ('bee') { $_ = "hurrah" }
default { "what?" }
};
say $_;
}
moo("bee");
On 22 October 2012 16:39, Toby Corkindale <
toby.corkindale at strategicdata.com.au> wrote:
> 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.
>
> ______________________________**_________________
> Melbourne-pm mailing list
> Melbourne-pm at pm.org
> http://mail.pm.org/mailman/**listinfo/melbourne-pm<http://mail.pm.org/mailman/listinfo/melbourne-pm>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/melbourne-pm/attachments/20121022/921c526b/attachment.html>
More information about the Melbourne-pm
mailing list