[Israel.pm] Correct way of taking a slice of a hash reference

Amir E. Aharoni amir.aharoni at gmail.com
Mon Jan 21 00:11:34 PST 2008


On 21/01/2008, Adam BE <adamb_e at yahoo.com> wrote:
> Using @$hash_ref{ @keys } I get a "@$hash_ref Not an ARRAY reference" warning in Komodo
>
> however I get no warning when running the same code with use strict and use warnings
> in the Perl debugger.

The best practice according to the Perl Best Practices book is to use
@{ $hash_ref }{ @keys }. (Pages 227-229).

Your code seems to be perfectly good. Maybe Komodo is silly and
doesn't know about hash slice pretending to be arrays.

Komodo is not the only tool that disregards this. Perl::Critic warns
about using %$hash_ref, but not about @$hash_ref.

(Your question reminded me of my first days of learning Perl. I
somehow figured out by myself that it makes sense that hash slices are
accessed as arrays. I don't understand why do so many people complain
the sigils...)

-- 
Amir Elisha Aharoni

English -  http://aharoni.wordpress.com
Hebrew  - http://haharoni.wordpress.com

"We're living in pieces,
 I want to live in peace." - T. Moore



More information about the Perl mailing list