Re: fun with hashes!

[email protected] (David Landgren) Sat, 24 Nov 2007 19:53:47 +0100
Newsgroups perl.fwp
Message-ID <[email protected]>
Uri Guttman writes:

>   DL> Why stop there? Assuming $key never evaluates to 0:
> 
>   DL>    my $sub = $dispatch{ $key || 'default' };
> 
> and what if $key is true but not found?? that is a different
> problem. your code doesn't handle that, it only handles a false or missing key
> 
>   DL> If it does, wait until 5.10 comes out and:
> 
>   DL>    my $sub = $dispatch{ $key // 'default' };
> 
> same problem.
> 
>   DL> Although there really is little point stuffing the coderef into a
>   DL> scalar, it's not like there's anything you can do to it. It's clearer to
>   DL> not draw attention to it and just run the damned thing:
> 
>   DL>    $dispatch{ $key || 'default' }->();
> 
> there are places where you want to delay the call. you may need to pass
> the code ref around or call it multiple times or with different args. i
> have found that getting the sub is better as a separate step from
> calling it.

I see what you and Aristotle are saying. In my own code I arrange things 
so that you're never actually feeding random strings into the dispatch 
table, I make sure the values are restricted to a finite domain 
beforehand, so I can't say I've been bitten by that.

And point taken re: calling it multiple times.

David