Re: code line of the day

[email protected] (John Douglas Porter) Fri, 8 Sep 2006 08:19:21 -0700 (PDT)
Newsgroups perl.fwp
Message-ID <[email protected]>
"A. Pagaltzis" <[email protected]> wrote:
>     sub flatten_copy {
>         local $_ = shift;
>         ref $_ eq 'SCALAR' ? "$$_" : "$_";
>     }
> 
>     my %copy = %$tmpls;
>     $_ = \( flatten_copy $_ ) for values %copy;
>     @{$self->{templates}}{ keys %copy } = values %copy;

Excellent!

I believe it's better to do

  ref $_ ? "$$_" : "$_"

than

  ref $_ eq 'SCALAR' ? "$$_" : "$_"

because, in the first case, you get an actual error
in the exceptional case (a ref of the wrong type),
whereas in the latter case you get a useless and
wrong string in the value, which you might not catch
until later, if ever.

If you really want to test the ref type, do so
robustly using the methods in Scalar::Util.

-- 
John Douglas Porter


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com