tags:

views:

771

answers:

6

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?

+7  A: 

To directly answer your question, the answer is at the bottom of the page you linked to, namely that if a package defines an isa method, then calling UNIVERSAL::isa directly will not call the package isa method. This is very unintuitive behaviour from an object-orientation point of view.

The rest of this post is just more questions about why you're doing this in the first place.

In code like the above, in what cases would that specific isa test fail? i.e., if it's a method, in which case would the first argument not be the package class or an instance thereof?

I ask this because I wonder if there is a legitimate reason why you would want to test whether the first argument is an object in the first place. i.e., are you just trying to catch people saying FooBar::method instead of FooBar->method or $foobar->method? I guess Perl isn't designed for that sort of coddling, and if people mistakenly use FooBar::method they'll find out soon enough.

Your mileage may vary.

Chris Jester-Young
+9  A: 

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. :)

Penfold
Thanks for explaining "why not" better than me! +1
Chris Jester-Young
+21  A: 

The primary problem is that if you call UNIVERSAL::isa directly, you are bypassing any classes that have overloaded isa. If those classes rely on the overloaded behavior (which they probably do or else they would not have overridden it), then this is a problem. If you invoke isa directly on your blessed object, then the correct isa method will be called in either case (overloaded if it exists, UNIVERSAL:: if not).

The second problem is that UNIVERSAL::isa will only perform the test you want on a blessed reference just like every other use of isa. It has different behavior for non-blessed references and simple scalars. So your example that doesn't check whether $ref is blessed is not doing the right thing, you're ignoring an error condition and using UNIVERSAL's alternate behavior. In certain circumstances this can cause subtle errors (for example, if your variable contains the name of a class).

Consider:

use CGI;

my $a = CGI->new();

my $b = "CGI";

print UNIVERSAL::isa($a,"CGI");  # prints 1, $a is a CGI object.
print UNIVERSAL::isa($b,"CGI");  # Also prints 1!! Uh-oh!!

So, in summary, don't use UNIVERSAL::isa... Do the extra error check and invoke isa on your object directly.

Adam Bellaire
UNIVERSAL::isa does NOT require a blessed reference. It works great on unblessed references also (e.g., UNIVERSAL::isa([7],'ARRAY') returns 1). As explained in the docs, if you pass it a string, it treats it as a class name (as demonstrated by your example).
cjm
@cjm: You're right, I should clarify that.
Adam Bellaire
+2  A: 

Assuming your example of what you want to be able to do is within an object method, you're being unnecessarily paranoid. The first passed item will always be either a reference to an object of the appropriate class (or a subclass) or it will be the name of the class (or a subclass). It will never be a reference of any other type, unless the method has been deliberately called as a function. You can, therefore, safely just use ref to distinguish between the two cases.

if (ref $_[0]) {
  my $self = shift;
  # called on instance, so do instancey things
} else {
  my $class = shift;
  # called as a class/static method, so do classy things
}
Dave Sherohman
+5  A: 

Everyone else has told you why you don't want to use UNIVERSAL::isa, because it breaks when things overload isa. If they've gone to all the habit of overloading that very special method, you certainly want to respect it. Sure, you could do this by writing:

if (eval { $foo->isa("thing") }) {
     # Do thingish things
}

because eval guarantees to return false if it throws an exception, and the last value otherwise. But that looks awful, and you shouldn't need to write your code in funny ways because the language wants you to. What we really want is to write just:

if ( $foo->isa("thing") ) {
     # Do thingish things
}

To do that, we'd have to make sure that $foo is always an object. But $foo could be a string, a number, a reference, an undefined value, or all sorts of weird stuff. What a shame Perl can't make everything a first class object.

Oh, wait, it can...

use autobox;   # Everything is now a first class object.
use CGI;       # Because I know you have it installed.

my $x = 5;
my $y = CGI->new;

print "\$x is a CGI object\n" if $x->isa('CGI');   # This isn't printed.
print "\$y is a CGI object\n" if $y->isa('CGI');   # This is!

You can grab autobox from the CPAN. You can also use it with lexical scope, so everything can be a first class object just for the files or blocks where you want to use ->isa() without all the extra headaches. It also does a lot more than what I've covered in this simple example.

pjf
Personally I prefer using `eval { $foo->isa("thing") }` rather than uncommon `autobox` module, which also affects performance.
codeholic
A: 

Right. It does a wrong thing for classes that overload isa. Just use the following idiom:

if (eval { $obj->isa($class) }) {

It is easily understood and commonly accepted.

codeholic