Alex Rivera | Logout

Why shouldn't I use UNIVERSAL::isa?

Asked 2008-10-15T10:57:46.983
17

According to this

http://perldoc.perl.org/UNIVERSAL.html

I shouldn't use UNIVERSAL::isa() and should instead use $obj->isa() or CLASS->isa().

This means that to find out if something is a reference in the first place and then is reference to this class I have to do

eval { $poss->isa("Class") }

and check $@ and all that gumph, or else

use Scalar::Util 'blessed';
blessed $ref && $ref->isa($class);

My question is why? What's wrong with UNIVERSAL::isa called like that? It's much cleaner for things like:

my $self = shift if UNIVERSAL::isa($_[0], __PACKAGE__)

To see whether this function is being called on the object or not. And is there a nice clean alternative that doesn't get cumbersome with ampersands and potentially long lines?

Edit
Report

1 Answer

9

See the docs for UNIVERSAL::isa and UNIVERSAL::can for why you shouldn't do it.

In a nutshell, there are important modules with a genuine need to override 'isa' (such as Test::MockObject), and if you call it as a function, you break this.

I have to say, my $self = shift if UNIVERSAL::isa($_[0], __PACKAGE__) doesn't look terribly clean to me - anti-Perl advocates would be complaining about line noise. :)

answered 2008-10-15T11:14:30.147

Your Answer