K
Koszalek Opalek
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
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