NAME
UNIVERSAL - base class for ALL classes (blessed references)
SYNOPSIS
$is_io = $fd->isa("IO::Handle");
$is_io = Class->isa("IO::Handle");
$sub = $obj->can("print");
$sub = Class->can("print");
$sub = eval { $ref->can("fandango") };
$ver = $obj->VERSION;
# but never do this!
$is_io = UNIVERSAL::isa($fd, "IO::Handle");
$sub = UNIVERSAL::can($obj, "print");
DESCRIPTION
UNIVERSAL is the base class from which all blessed references inherit. See perlobj.
UNIVERSAL provides the following methods:
$obj->isa( TYPE )CLASS->isa( TYPE )eval { VAL->isa( TYPE ) }-
Where
TYPE-
is a package name
$obj-
is a blessed reference or a string containing a package name
CLASS-
is a package name
VAL-
is any of the above or an unblessed reference
When used as an instance or class method (
$obj->isa( TYPE )),isareturns true if $obj is blessed into packageTYPEor inherits from packageTYPE.When used as a class method (
CLASS->isa( TYPE ), sometimes referred to as a static method),isareturns true ifCLASSinherits from (or is itself) the name of the packageTYPEor inherits from packageTYPE.If you're not sure what you have (the
VALcase), wrap the method call in anevalblock to catch the exception ifVALis undefined.If you want to be sure that you're calling
isaas a method, not a class, check the invocant withblessedfrom Scalar::Util first:use Scalar::Util 'blessed'; if ( blessed( $obj ) && $obj->isa("Some::Class") { ... } $obj->can( METHOD )CLASS->can( METHOD )eval { VAL->can( METHOD ) }-
canchecks if the object or class has a method calledMETHOD. If it does, then it returns a reference to the sub. If it does not, then it returns undef. This includes methods inherited or imported by$obj,CLASS, orVAL.cancannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overridencanappropriately), so a return value of undef does not necessarily mean the object will not be able to handle the method call. To get around this some module authors use a forward declaration (see perlsub) for methods they will handle via AUTOLOAD. For such 'dummy' subs,canwill still return a code reference, which, when called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef will cause an error.You may call
canas a class (static) method or an object method.Again, the same rule about having a valid invocant applies -- use an
evalblock orblessedif you need to be extra paranoid. VERSION ( [ REQUIRE ] )-
VERSIONwill return the value of the variable$VERSIONin the package the object is blessed into. IfREQUIREis given then it will do a comparison and die if the package version is not greater than or equal toREQUIRE.VERSIONcan be called as either a class (static) method or an object method.
EXPORTS
None by default.
You may request the import of all three functions (isa, can, and VERSION), however it is usually harmful to do so. Please don't do this in new code.
For example, previous versions of this documentation suggested using isa as a function to determine the type of a reference:
use UNIVERSAL 'isa';
$yes = isa $h, "HASH";
$yes = isa "Foo", "Bar";
The problem is that this code will never call an overridden isa method in any class. Instead, use reftype from Scalar::Util for the first case:
use Scalar::Util 'reftype';
$yes = reftype( $h ) eq "HASH";
and the method form of isa for the second:
$yes = Foo->isa("Bar");