I thought I'd stick with the basic technique of an if else chain, to keep things simple on the perl end of things. But, if you don't mind working with references (which are something every perl programmer should learn to use), a hash of subroutine refs is a most excellent technique.
my %lookup = (edit => \&let_the_user_edit_it,
preview => \&preview_it,
commit => \&commit_to_get_fit,
ERROR => \&non_existant_state );
if exists $lookup{param('state')}
{ $lookup{param('state')}->();
} else
{ $lookup{ERROR)}->();
}
I made a couple of changes to your code. Storing the subroutine addresses directly, saves one round of subroutine dispatch, a negligable performance savings, but it removes 1 level of indirection and saves on typing--to my mind, it's tidier. I also check for the existance of the hash key before trying to execute its code reference. This lets you give a better feedback in your error page than a plain 500 error usually does.
TGI says moo
|