JB> I noticed that the following works perfectly:
JB> my $value = ${{ foo => bar, baz => faz }}{ $type }
JB> which assigns bar when $type is foo, or faz when $type is baz.
JB> Of course this is more fun when you have a longer list and don't want to
JB> assign a hash to a variable and use it.
JB> If there is a (tested!) shorter way to do this, let me know
pretty much the same thing but using ->
{ foo => bar, baz => faz }->{ $type }
i have seen some cute hacks doing the same thing with array
refs. nothing that i would ever use in production. if the hash is ever
used more than one time it is a waste to build it each time through. if
it will always be used in the program (even once) it should be a static
hash declared and initialized outside the sub (or even in the sub but
named). i like dispatch tables and such but that style is too dense for
my taste. and if it ever grows beyond 2 entries it becomes very
fugly. also what about when $type isn't one of those two keys? if you
know it will always be one of them, you can reduce that to a simpler
conditional expression:
$type eq 'foo' ? 'bar' : 'faz' ;
even a 3 way choice is clean enough with conditionals:
$type eq 'foo' ? 'bar' :
$type eq 'baz' ? :'faz' : 'error' ;
damian conway has some wacky way he formats nested conditionals but i
can't recall it now.
also you didn't quote the values so that wasn't strict safe.
uri