in reply to Is there a way to find out what kind of ref an object is?

I'd probably use:

BEGIN { *isa= \&UNIVERSAL::isa; } ... isa( $ref, 'HASH' ) ...

which isn't perfect, but neither are any of the techniques I've seen that aren't unreasonably complicated, unfortunately.

- tye        

  • Comment on Re: Is there a way to find out what kind of ref an object is? (isa)
  • Download Code

Replies are listed 'Best First'.
Re^2: Is there a way to find out what kind of ref an object is? (isa)
by adrianh (Chancellor) on Dec 07, 2005 at 12:32 UTC
    neither are any of the techniques I've seen that aren't unreasonably complicated, unfortunately.

    Is:

    use Scalar::Util qw( reftype ); ... print "$ref is a ", reftype( $ref ), "\n";

    unreasonably complicated?

      I guess you are assuming that it is perfect. But it isn't. It prevents you from properly handling overloaded objects that know how to act like a hash ref even though they aren't.

      Of course, it is also overly complicated in many cases. For example, on the version of Perl I have most handy, it simply dies with Can't locate Scalar/Util.pm and overcoming that is rather hugely complicated, as it happens.

      - tye        

        But it isn't. It prevents you from properly handling overloaded objects that know how to act like a hash ref even though they aren't.

        I'd interpreted the OP's requirement as not wanting this behaviour. YMMV.

        on the version of Perl I have most handy, it simply dies with Can't locate Scalar/Util.pm and overcoming that is rather hugely complicated, as it happens

        Fair point.

      It has the misfortune of working correctly, as opposed to ref or UNIVERSAL::isa().

        It has the misfortune of working correctly, as opposed to ref or UNIVERSAL::isa().

        Oh I do agree that the number of times that you'd actually want to use reftype, outside pure curiosity and debugging, is pretty darn tiny. However, on the odd occasion when I do need it I find it fortunate to have something that does exactly the job I want and is named properly :-)