Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Re: How do you find keys of a Blessed Hash?

by moritz (Cardinal)
on Oct 07, 2012 at 14:51 UTC ( #997683=note: print w/replies, xml ) Need Help??

in reply to How do you find keys of a Blessed Hash?

$ perl -wE 'my $h = bless { a => 1, b => 2 }; say keys %$h' ab

Works just as with any other hash reference.

Replies are listed 'Best First'.
Re^2: How do you find keys of a Blessed Hash?
by perl-diddler (Chaplain) on Oct 07, 2012 at 15:01 UTC
    You're right... hmmm... works in simple case, ARG, but not where I'm using it... ooooo Might have something to do with my using Alias in the code .... gonna have to go ponder this some more...I thought it seemed to strange that keys didn't work -- thanks for the reality check!

      Instead of running off, again, on a wild goose chase, it maybe would be simpler to just read keys, the official documentation? It mentions there that:

      Starting with Perl 5.14, keys can take a scalar EXPR, which must contain a reference to an unblessed hash or array. The argument will be dereferenced automatically. This aspect of keys is considered highly experimental. The exact behaviour may change in a future version of Perl.

      ... and without seeing your code, my guess is that you are "using" that experimental feature, knowingly or not.

        It's sad that these new features we're getting never work as well as intended (given's lexical $_, the mess of smartmatch, auto-dereferencing with keys and others, treating arrays like hashes).


      Assuming Corion located the problem for you, then you need only dereference the hash yourself, rather than relying on keys to do it for you:

      $ cat #!/usr/bin/perl *STDERR = *STDOUT; $|=1; my $hr = bless {a=>1, b=>2}, "food"; print "Manual deref: ", join(", ", keys %$hr), "\n"; print "Auto deref: ", join(", ", keys $hr), "\n"; $ perl Manual deref: a, b Type of argument to keys on reference must be unblessed hashref or arr +ayref at line 8.

      Update: Fixed formatting.


      When your only tool is a hammer, all problems look like your thumb.

        That's actually the problem -- I didn't go run off and try things... Got caught up in something else, as the explanation I got didn't make immediate sense, so... that's when I usually go do something else for a while to let ideas percolate around... that -- and in this case, see what other ideas pop up. I had a ref to a hash---and I left off the % on the front. Gonna have to watch for that one -- the new error message didn't exactly tell me what I'd done the way the old one did. Just have to remember when I see that new error message I should check for typos...Thanks for the followup...

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997683]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-06-20 04:24 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (116 votes). Check out past polls.