Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Checking if an object inherits from class T

0 views
Skip to first unread message

Koszalek Opalek

unread,
Jun 14, 2008, 1:45:55 AM6/14/08
to
I am using objects of class T and objects of
other classes that inherit from T. Somewhere
in my code I need to check if an object is of
class T or of any other class that inherits
from T.

The following
if( ref $obj eq 'T' )
(obviously) works only for objects of class T but
not for objects blessed to other classes that
inherit from T.


I have come up with two solutions:

1) add the following method to T:
sub isT {};
and then check
if ( $obj->can( 'isT' )

This works except that it is two times slower
that checking the ref (i.e. if (ref $obj eq 'T'. )


2) add a field isT to the object in the constructor
and than check
if ( $obj->{isT} )
This is as fast as checking the ref but it looks
ugly and duplicates the field across all objects.


Is there a more elegant way that would be as fast
as checking the ref?


Koszalek

Christian Winter

unread,
Jun 14, 2008, 9:29:29 AM6/14/08
to
Koszalek Opalek wrote:
> I am using objects of class T and objects of
> other classes that inherit from T. Somewhere
> in my code I need to check if an object is of
> class T or of any other class that inherits
> from T.

You can use Class->isa() (see "perldoc UNIVERSAL"):

if( ref($obj)->isa('T') )
{
# class is T or inherits from it
}

HTH
-Chris

Ben Morrow

unread,
Jun 14, 2008, 11:55:10 AM6/14/08
to

Quoth Christian Winter <thepoet...@arcor.de>:

*NO*. Use $obj->isa('T') directly, otherwise objects can't override it
if they need to.

Ben

--
The cosmos, at best, is like a rubbish heap scattered at random.
Heraclitus
b...@morrow.me.uk

brian d foy

unread,
Jun 15, 2008, 10:29:09 AM6/15/08
to
In article <4853c7d5$0$6554$9b4e...@newsspool3.arcor-online.net>,
Christian Winter <thepoet...@arcor.de> wrote:

actaully, you want to do

if( eval { $obj->isa("T") } )

Call isa() directly on the object, and don't care too much about what
is actaully in $obj. If it's not an object, you just get back false,
which is still the right nswer to the question. :)

Koszalek Opalek

unread,
Jun 16, 2008, 1:51:50 AM6/16/08
to
On Jun 15, 4:29 pm, brian d foy <brian.d....@gmail.com> wrote:
> In article <4853c7d5$0$6554$9b4e6...@newsspool3.arcor-online.net>,

> actaully, you want to do
>
>    if( eval { $obj->isa("T") } )
>
> Call isa() directly on the object, and don't care too much about what
> is actaully in $obj. If it's not an object, you just get back false,
> which is still the right nswer to the question. :)

I fixed my code and then bumped in this very problem
(i.e. Can't call method "isa" without a package or object
reference). I come to perl.misc and there is an answer
before I even asked the question :D.

K.

Koszalek Opalek

unread,
Jun 16, 2008, 2:52:01 PM6/16/08
to
On Jun 15, 3:29 pm, brian d foy <brian.d....@gmail.com> wrote:
> In article <4853c7d5$0$6554$9b4e6...@newsspool3.arcor-online.net>,
>

> actaully, you want to do


>
> if( eval { $obj->isa("T") } )
>
> Call isa() directly on the object, and don't care too much about what
> is actaully in $obj. If it's not an object, you just get back false,
> which is still the right nswer to the question. :)

The only thing is that it is approximately 4x slower on my
simple benchmark compared to just checking the ref, i.e.


if( ref $obj eq 'T' )

But that's the way it has to stay.

K.

0 new messages