Front page | perl.fwp |
Postings from November 2007
Re: fun with hashes!
Thread Previous
|
Thread Next
From:
Mr. Shawn H. Corey
Date:
November 23, 2007 15:47
Subject:
Re: fun with hashes!
Message ID:
47476629.70303@magma.ca
Uri Guttman wrote:
>>>>>> "AP" == A Pagaltzis <pagaltzis@gmx.de> writes:
>
> AP> * Jerrad Pierce <belg4mit@MIT.EDU> [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 /
+------------/
Thread Previous
|
Thread Next