SPUG: Hash of subs Re: Hashes and subroutines

Fred Morris m3047 at inwa.net
Mon Jan 5 18:56:47 PST 2009


Sure, I do it all the time.

%H = ( play => sub {}, that=> sub {}, funky=> sub{}, music=> sub{} );

&{$H{play}}('awb');

Somebody will point out how to do it without so many braces.

Maybe somebody will step up and show how to do it with the hash blessed and 
the subs as autoload methods. ;-) Too complicated for my needs though.

--

Fred

On Monday 05 January 2009 16:07, Christopher Howard wrote:
> 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? [...]


More information about the spug-list mailing list