Uri Guttman wrote:
"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' } } ;

Shouldn't this be?

 my $sub = $dispatch{ $dispatch{ $key }
                    ? $key
                    : '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


Or:

 my $sub = ( exists $dispatch{ $key } && ref( $dispatch{ $key } ) eq 'CODE' )
         ? $dispatch{ $key }
         : $dispatch{ 'default' };

Just because you're not paranoid doesn't mean computers don't hate you :)
--
Just my 0.00000002 million dollars worth,
 Shawn

+------------\
| Shangri La  \
| 40,000 km   /
+------------/

Reply via email to