Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re^2: I don't understand UNIVERSAL::DOES() (hash method?!)

by tye (Sage)
on Mar 08, 2007 at 21:52 UTC ( [id://603899]=note: print w/replies, xml ) Need Help??


in reply to Re: I don't understand UNIVERSAL::DOES()
in thread I don't understand UNIVERSAL::DOES()

Consider, for example, a normal hash reference, a blessed hash, a tied hash reference, and a blessed scalar with hash dereferencing overloading.
[....] You could use UNIVERSAL:­:isa() to check if the entity seems to be a built-in hash (but that fails for the last two items and it's really bad style).

I believe and my testing confirms that UNIVERSAL::isa() actually works in all but the last case, not just 2 of 4 as you claim.

I find it ironic, if this is meant to be used for what you claim, that it has repeated the mistake of expecting people to use methods on hash references when methods can't be used on normal hash references (Perl dies due to "unblessed reference"), not even on a reference to an (unblessed) tied hash (so 2 of your 4 cases, including the by-far most common one).

So it appears we have yet another inconvenient and/or incomplete way to check whether something can be used as a hash. I'll certainly be sticking with UNIVERSAL::isa() for much of my simpler code since it is many times simpler than any of the (possibly) more accurate alternatives, it covers the vast majority of cases, and objects for which it fails can easily be fixed such that it works for them as well; plus it works on non-bleeding-edge versions of Perl.

- tye        

Replies are listed 'Best First'.
Re^3: I don't understand UNIVERSAL::DOES() (hash method?!)
by chromatic (Archbishop) on Mar 08, 2007 at 22:00 UTC
    So it appears we have yet another inconvenient and/or incomplete way to check whether something can be used as a hash.

    The hash example was an analogy--an analogy of a different type of allomorphism already present in Perl 5.

      Thanks.

      - tye        

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (5)
As of 2024-03-29 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found