>>>>> "AP" == A Pagaltzis <[EMAIL PROTECTED]> writes:
AP> * Jerrad Pierce <[EMAIL PROTECTED]> [2007-11-23 22:50]:
>> exists( $dispatch{$sub} ) ? $dispatch{$sub}->() :
>> warn "Key <$sub> does not exist in the dispatch table";
AP> ( $dispatch{$sub} || sub { warn "no such action '$sub'" } )->();
some variations on that:
my $sub = $dispatch{$key} or die "trying to call missing code" ;
$sub->() ;
or:
my %dispatch = (
foo => \&foo,
...
default => \&default
) ;
$key = 'default' unless exists( $dispatch{ $key } ;
$sub = $dispatch{ $key } ;
or:
my $sub = $dispatch{ $dispatch{ $key } ?
$key : $dispatch{ 'default' } } ;
or:
my $sub = $dispatch{ $key } || $dispatch{ 'default' } ;
as you can see there are various ways to handle a missing dispatch
key. this is not a hash issue but a detail about doing dispatch tables
cleanly. sometimes you want to predefine a default code ref in the
dispatch table and other times you want to handle the missing key when
you make the call through the dispatch table.
uri
--
Uri Guttman ------ [EMAIL PROTECTED] -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org