UNIVERSAL - base class for ALL classes (blessed references)
$is_io = $fd->isa("IO::Handle");$is_io = Class->isa("IO::Handle");$does_log = $obj->DOES("Logger");$does_log = Class->DOES("Logger");$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");
UNIVERSAL
is the base class from which all blessed references inherit. Seeperlobj.
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 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 )
),isa
returnstrue if $obj is blessed into packageTYPE
or inherits from packageTYPE
.
When used as a class method (CLASS->isa( TYPE )
, sometimes referred to as a static method),isa
returnstrue ifCLASS
inherits from (or is itself) the name of the packageTYPE
or inherits from packageTYPE
.
If you're not sure what you have (theVAL
case), wrap the method call in aneval
block to catch the exception ifVAL
is undefined.
If you want to be sure that you're callingisa
as a method, not a class, check the invocant withblessed
fromScalar::Util first:
use Scalar::Util 'blessed';if ( blessed( $obj ) && $obj->isa("Some::Class") { ...}
$obj->DOES( ROLE )
CLASS->DOES( ROLE )
DOES
checks if the object or class performs the roleROLE
. A role is a named group of specific behavior (often methods of particular names and signatures), similar to a class, but not necessarily a complete class by itself. For example, logging or serialization may be roles.
DOES
andisa
are similar, in that if either is true, you know that the object or class on which you call the method can perform specific behavior. However,DOES
is different fromisa
in that it does not carehow the invocant performs the operations, merely that it does. (isa
of course mandates an inheritance relationship. Other relationships include aggregation, delegation, and mocking.)
By default, classes in Perl only perform theUNIVERSAL
role, as well as the role of all classes in their inheritance. In other words, by defaultDOES
responds identically toisa
.
There is a relationship between roles and classes, as each class implies the existence of a role of the same name. There is also a relationship between inheritance and roles, in that a subclass that inherits from an ancestor class implicitly performs any roles its parent performs. Thus you can useDOES
in place ofisa
safely, as it will return true in all places whereisa
will return true (provided that any overriddenDOES
andisa
methods behave appropriately).
$obj->can( METHOD )
CLASS->can( METHOD )
eval { VAL->can( METHOD ) }
can
checks 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 returnsundef. This includes methods inherited or imported by$obj
,CLASS
, orVAL
.
can
cannot know whether an object will be able to provide a method through AUTOLOAD (unless the object's class has overridencan
appropriately), so a return value ofundef 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 (seeperlsub) for methods they will handle via AUTOLOAD. For such 'dummy' subs,can
will 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 callcan
as a class (static) method or an object method.
Again, the same rule about having a valid invocant applies -- use aneval
block orblessed
if you need to be extra paranoid.
VERSION ( [ REQUIRE ] )
VERSION
will return the value of the variable$VERSION
in the package the object is blessed into. IfREQUIRE
is given then it will do a comparison and die if the package version is not greater than or equal toREQUIRE
.
VERSION
can be called as either a class (static) method or an object method.
NOTE:can
directly uses Perl's internal code for method lookup, andisa
uses a very similar method and cache-ing strategy. This may cause strange effects if the Perl code dynamically changes @ISA in any package.
You may add other methods to the UNIVERSAL class via Perl or XS code. You do not need touse UNIVERSAL
to make these methods available to your program (and you should not do so).
None by default.
You may request the import of three functions (isa
,can
, andVERSION
), however it is usually harmful to do so. Please don't do this in new code.
For example, previous versions of this documentation suggested usingisa
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 willnever call an overriddenisa
method in any class. Instead, usereftype
fromScalar::Util for the first case:
use Scalar::Util 'reftype';$yes = reftype( $h ) eq "HASH";
and the method form ofisa
for the second:
$yes = Foo->isa("Bar");
Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.
The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.