develooper Front page | perl.fwp | Postings from November 2007

Re: fun with hashes!

Thread Previous | Thread Next
From:
David Landgren
Date:
November 24, 2007 01:44
Subject:
Re: fun with hashes!
Message ID:
4747F25E.3040401@landgren.net
Uri Guttman writes:
>>>>>> "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:

[...]

> or:
> 
> 	my $sub = $dispatch{ $key } || $dispatch{ 'default' } ;


Why stop there? Assuming $key never evaluates to 0:

   my $sub = $dispatch{ $key || 'default' };

If it does, wait until 5.10 comes out and:

   my $sub = $dispatch{ $key // 'default' };

Although there really is little point stuffing the coderef into a 
scalar, it's not like there's anything you can do to it. It's clearer to 
not draw attention to it and just run the damned thing:

   $dispatch{ $key || 'default' }->();

David

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About