SPUG: Hashes and subroutines

Christopher Howard choward at indicium.us
Mon Jan 5 16:07:59 PST 2009


I've got this situation where I execute a certain subroutine based on the 
value of a variable, like so:

if($var eq 'play') { sub1() }
elsif($var eq 'that') { sub2() }
elsif($var eq 'funky') { sub3() }
elsif($var eq 'music') { sub4() }
...

Is there some way to do this with a hash instead? Say, the possible values 
of $var are the keys in the hash, and subroutine names (or references or 
whatever) are the values in the hash? It'd be nice if I could write out 
one long hash, and then a small piece of code to execute the 
appropriate subroutine, instead of 20+ elsif statements.

-- 
Christopher Howard
http://indicium.us


More information about the spug-list mailing list