Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
more useful options
 
PerlMonks  

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

by perl-diddler (Friar)
on Oct 07, 2012 at 15:01 UTC ( #997685=note: print w/ replies, xml ) Need Help??


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

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!


Comment on Re^2: How do you find keys of a Blessed Hash?
Re^3: How do you find keys of a Blessed Hash?
by Corion (Pope) on Oct 07, 2012 at 15:06 UTC

    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).

        keys $blessed doesn't work for a very good reason. keys can (in recent versions of Perl) be used with either arrays or hashes. A blessed object can (thanks to overload) be simultaneously dereferencable as a hash and and array. So for blessed objects, you need to manually dereference.

        perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re^3: How do you find keys of a Blessed Hash?
by roboticus (Canon) on Oct 07, 2012 at 16:56 UTC

    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://997685]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-04-20 04:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (485 votes), past polls