Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

Eh, ref and reftype both have their problems. My preference is to use:

*isa= UNIVERSAL::isa; sub whatever { for my $ref ( @_ ) { if( isa( $ref, "HASH" ) ) { # ...

Because it doesn't fail to be useful just because a reference is blessed (like ref does), it is simple, and it is easy to fool it into doing the right thing when it doesn't get it right by default. reftype() won't work for overloaded objects that can behave as a certain type of reference whether or not they are implemented as that type of reference. isa doesn't (by default) handle that case either, but you at least can make your overloaded object inherit from "HASH" and/or "ARRAY" (etc.) in order to get isa() to "do the right thing".

There really needs to be some "canBe" added to the Perl core that correctly handles all of these cases. I'd write or search for a module to determine allowed reference types that was aware of overload, but such not being in the core would make it fairly useless to me, personally.

On the rare occassion when I really want to do the extra effort in order to get this type of thing right (rather than doing something simple that works in most cases and can be fooled to work in the others), I use eval (as with Data::Diver):

sub whatever { for my $ref ( @_ ) { if( !ref($ref) ) { # Not a reference } elsif( eval { my $x= $ref->{''}; 1 } ) { # It can be used as a hash ref # ...

But that isn't absolutely perfect because it will invoke a FETCH method that might be broken enough to change some state somewhere, making our test intrusive. But I can at least shift some of the blame to the author of the "broken" FETCH method, so it is the best I've implemented.

Note that I didn't go the route of figuring out how to decide if overload.pm was providing the functionality as it wasn't too long ago when this stuff changed and my eval trick will work even if it changes again. Better would be a core module that safely makes the determination and stays up-to-date.

Note that some hate the use of isa() because it can be fooled into doing the wrong thing. I don't see that as a problem in most situations, and sometimes it can be useful. But you should be aware of that.

- tye        


In reply to Re^2: Determine Reference Types (TIMTOWWTDI) by tye
in thread Determine Reference Types by abachus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (9)
As of 2024-03-28 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found