How to dereference a hashref constant?

Posted by makenai on Stack Overflow See other posts from Stack Overflow or by makenai
Published on 2010-04-16T20:32:52Z Indexed on 2010/04/16 20:43 UTC
Read the original article Hit count: 275

Filed under:

Let's say I have a hashref constant like the following:

use constant DOGS => {
    Lassie => 'collie',
    Benji  => 'mutt',
    Scooby => 'great dane',
    Goofy  => '???'
};

How can I dereference it properly to get say.. they keys out of it?

warn ref DOGS; # HASH at untitled line 12.
warn keys( %{DOGS} ); # Warning: something's wrong (empty list)
warn keys( DOGS ); # Type of arg 1 to keys must be hash (not constant item)

The following is the only way I can seem to make it work:

my $dogs = DOGS;
warn keys( %$dogs ); # LassieBenjiGoofyScooby at untitled line 15.

What am I doing wrong?

© Stack Overflow or respective owner

Related posts about perl