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?
The primary problem is that if you call
UNIVERSAL::isadirectly, you are bypassing any classes that have overloadedisa. 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 invokeisadirectly on your blessed object, then the correctisamethod will be called in either case (overloaded if it exists, UNIVERSAL:: if not).The second problem is that
UNIVERSAL::isawill only perform the test you want on a blessed reference just like every other use ofisa. It has different behavior for non-blessed references and simple scalars. So your example that doesn’t check whether$refis blessed is not doing the right thing, you’re ignoring an error condition and usingUNIVERSAL‘s alternate behavior. In certain circumstances this can cause subtle errors (for example, if your variable contains the name of a class).Consider:
So, in summary, don’t use
UNIVERSAL::isa… Do the extra error check and invokeisaon your object directly.