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

Daniel Pittman daniel at rimspace.net
Tue Jun 1 22:48:09 PDT 2010


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.

        Daniel

-- 
✣ Daniel Pittman            ✉ daniel at rimspace.net            ☎ +61 401 155 707
               ♽ made with 100 percent post-consumer electrons


More information about the Melbourne-pm mailing list