Re: fun with hashes!

[email protected] (Vladi Belperchinov-Shabanski) Sat, 24 Nov 2007 15:01:50 +0200
Newsgroups perl.fwp
Organization DataMax
Message-ID <[email protected]>
On Sat, 24 Nov 2007 10:43:58 +0100
David Landgren <[email protected]> wrote:

> 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' };

usual case would be there is no value (coderef) for the key, not empty key!

> 
> 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 

little? really? I'd rather handle situation when $sub is empty.
shuldn't happen or get logged (debug obviously) for example.

> 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' }->();
> 

Which still may trigger "Can't use string ("") as a subroutine ref while "strict refs" in use at ..."

:)

> David
> 


-- 
Vladi Belperchinov-Shabanski <[email protected]> <[email protected]>
http://cade.datamax.bg/   pgp/gpg key id: 6F35B214 (pgp.mit.edu)
--
equally destructive as we are, don't you think we've also gone too far?!
signature.asc (application/pgp-signature, 189 B)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (GNU/Linux)

iD8DBQFHSCC+kBm4tG81shQRAkivAJ0dkO3dH6oT9g741uKqxCtbuQmqQgCgotUC
GIGxVwyimmNwe3bVqvGLKbM=
=sV1K
-----END PGP SIGNATURE-----