http://qs321.pair.com?node_id=32316


in reply to cursebox v0.9 - "The Cursed Jukebox"

Update: Please use strict; !

Though I have not used this code, I have one comment to make in the spirit of TIMTOWTDI (hopefully a good comment) - I find the simulated switch statement to map a keypress to an action rather clumsy, as it does not really allow to easily add other actions later or add other actions dynamically.

In such situations, I prefer to use a hash of code references, like this :

my %actions = ( "n" => \&skip, "m" => \&movesong, # ... rest ommited ... "\n" => \&goto, );
and the routine interpretplay() would then look like this (sadly, untested) :
sub interpretplay { if (exists $actions{$char}) { &$actions{$char}; } else { &invalid; }; };
or, if the whole state machine would be changed to use hashes, one could even use a single interpret() method that would simply look at the $current_state hash reference to find the action to be executed.

Dynamically loaded actions would then simply modify the %actions hash with their entry.