Re: fun with hashes!
"A. Pagaltzis" <[email protected]> Sat, 24 Nov 2007 16:10:17 +0100
| Newsgroups | gmane.comp.lang.perl.fun |
|---|---|
| Message-ID | <20071124151017.GH20669@klangraum> |
* David Landgren <[email protected]> [2007-11-24 10:45]: > Uri Guttman writes: >>>>>>> "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: > > [...] > >> or: >> my $sub = $dispatch{ $key } || $dispatch{ 'default' } ; > > Why stop there? Assuming $key never evaluates to 0: > > my $sub = $dispatch{ $key || 'default' }; Your dispatch table has keys for all nonzero strings? That’s one big hash. Regards, -- Aristotle Pagaltzis // <http://plasmasturm.org/>