Re: fun with hashes!

Uri Guttman <[email protected]> Fri, 23 Nov 2007 17:51:49 -0500
Newsgroups gmane.comp.lang.perl.fun
Message-ID <[email protected]>
>>>>> "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