Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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.


Comment on Re: How do you find keys of a Blessed Hash?
Download Code
Re^2: How do you find keys of a Blessed Hash?
by perl-diddler (Hermit) 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).

      perl-diddler:

      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 test.pl #!/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 test.pl Manual deref: a, b Type of argument to keys on reference must be unblessed hashref or arr +ayref at test.pl line 8.

      Update: Fixed formatting.

      ...roboticus

      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://997683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2014-09-16 22:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (51 votes), past polls