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?!
pgpZRpQuqgczo.pgp
Description: PGP signature
