UNIVERSAL: The Ultimate Ancestor Class
If no method definition with the right name is found after searching
the invocant’s class and all its ancestor classes recursively, one more
check for a method of that name is made in the special predefined class
called UNIVERSAL. This package never appears in an @ISA, but it is always consulted when an
@ISA check fails. You can think of
UNIVERSAL as the ultimate ancestor
from which all classes implicitly derive, making it work like class
Object does in Java or class object in Python’s new-style classes.
The following predefined methods are available in class UNIVERSAL, and thus in all classes. These all
work regardless of whether they are invoked as class methods or object
methods.
INVOCANT–>isa(CLASS)The
isamethod returns true ifINVOCANT’s class isCLASSor any class inheriting fromCLASS. Instead of a package name,CLASSmay also be one of the built-in types, such as “HASH” or “ARRAY”. (Checking for an exact type does not bode well for encapsulation or polymorphism, though. You should be relying on method dispatch to give you the right method.)use IO::Handle; if (IO::Handle–>isa("Exporter")) { print "IO::Handle is an Exporter.\n"; } $fh = IO::Handle–>new(); if ($fh–>isa("IO::Handle")) { print "\$fh is some sort of IOish object.\n"; } if ($fh–>isa("GLOB")) { print "\$fh is really a GLOB reference.\n"; }INVOCANT–>DOES(ROLE)Perl v5.10 added the idea of roles, a way that a class can include external methods without necessarily inheriting them, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access