Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I'd like to interrogate any little objects I come across and ask them to identify themselves, i.e I'd like them to tell me their class. I thought they're might be a
$unidentifed_object->CLASS
method or somesuch but I've not found anything. Is there a way?

Replies are listed 'Best First'.
Re: Asking An Object For It's Class
by demerphq (Chancellor) on Aug 14, 2003 at 01:16 UTC

    TMTOWTDI:

    use Scalar::Util qw(blessed); if (my $class=blessed $foo) { print "Its an object of class $class\n"; } else { print "Its not an object\n"; }

    This has the advantage that you can discriminate a blessed reference from an un-blessed one safely, regardless as to what package name it has been blessed into, and avoids the painful filtering of unblessed references which requires overload::StrVal and regexes if you want to be thorough. There is also a function called reftype() which does the converse, telling you what the underlying type is and not what type its blessed into.

    Testing '$Evil_Hash' ref :ARRAY U::isa :1 ->isa :1 StrVal :ARRAY=HASH(0x1abf1a8) blessed :ARRAY reftype :HASH refaddr :28045736 ----

    ---
    demerphq

    <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: Asking An Object For It's Class
by Limbic~Region (Chancellor) on Aug 13, 2003 at 22:54 UTC
    Anonymous Monk,
    Unless there has been any hanky panky going on, I believe that a simple ref $object; should do what you want? If you suspect foul play or want to know the underlying type of an object, I would recommend reading this node. I found it quite enlightening.

    <headshake>my $not_a_hash = bless [], 'HASH';</headshake>

    Cheers - L~R

Re: Asking An Object For It's Class
by shemp (Deacon) on Aug 13, 2003 at 22:57 UTC
    Here's 2 approaches that may get you what you want. 1 is to ref() the variable, and it will tell you something about the object.
    my $what_is_it = ref($unidentified_object);
    This may not return what you expect though, because most objects are of more than 1 type (they are whatever they were last bless()'ed as, and all of their parent class types). Someone else can certainly better explain exactly what ref() will return in various circumstances.

    A probably better options is to use UNIVERSAL::isa(), which all classes inherit. isa() allows you to ask an object if its a certain type:
    if ( $u_o->isa("blah" ) ) { # do something } elsif ( $u_o->isa("something_else") ) { # do something else } ...
    Depending on what you're trying to do, one or the other should probably do what you need.
Re: Asking An Object For It's Class
by larsen (Parson) on Aug 13, 2003 at 22:55 UTC
    From perldoc -f ref
    If the referenced object has been blessed into a package, then that package name is returned instead. You can think of "ref" as a "typeof" operator.
    So it's
    my $class = ref( $unidentified_object );
Re: Asking An Object For It's Class
by particle (Vicar) on Aug 13, 2003 at 22:54 UTC

    perldoc -f ref

    ~Particle *accelerates*

Re: Asking An Object For It's Class
by adrianh (Chancellor) on Aug 13, 2003 at 22:55 UTC