[Melbourne-pm] Using given/when to return values

Toby Corkindale toby.corkindale at strategicdata.com.au
Tue Jun 1 22:55:11 PDT 2010


On 02/06/10 15:48, Daniel Pittman wrote:
> Toby Corkindale<toby.corkindale at strategicdata.com.au>  writes:
>> On 02/06/10 15:16, Toby Corkindale wrote:
>>
>>> An annoyance right now is that the given/when syntax does not return the
>>> values in the code block executed, so you can't use them inside grep,etc.
>>
>> Although thinking about this for slightly longer, there's no reason to have
>> anything other than boolean matching inside things like grep and it's rather
>> dubious for sort, so perhaps this isn't such a big issue.
>
> Honestly?  I see a big case for this:
>
> sub foo {
>     my ($arg_to_map) = @_;
>     return given ($arg_to_map) {
>         when (1..4) { "foo" }
>         when (5..8) { "bar" }
>         default { explode; }
>     }
> }
>
> Sure, I can do assignments in the when blocks and all, but this beats out the
> simpler static case where a function wraps a hash lookup to the same end.

I agree, it would be handy, and it seems like a big oversight to define 
given as invalid if used as an RVALUE.

ie.
my $result = given ($thing) ...etc
results in: syntax error near "= given"

(Tested on Perl 5.10.1 and 5.12.1)


More information about the Melbourne-pm mailing list