1UNIVERSAL(3pm)         Perl Programmers Reference Guide         UNIVERSAL(3pm)
2
3
4

NAME

6       UNIVERSAL - base class for ALL classes (blessed references)
7

SYNOPSIS

9           $is_io    = $fd->isa("IO::Handle");
10           $is_io    = Class->isa("IO::Handle");
11
12           $does_log = $obj->DOES("Logger");
13           $does_log = Class->DOES("Logger");
14
15           $sub      = $obj->can("print");
16           $sub      = Class->can("print");
17
18           $sub      = eval { $ref->can("fandango") };
19           $ver      = $obj->VERSION;
20
21           # but never do this!
22           $is_io    = UNIVERSAL::isa($fd, "IO::Handle");
23           $sub      = UNIVERSAL::can($obj, "print");
24

DESCRIPTION

26       "UNIVERSAL" is the base class from which all blessed references
27       inherit.  See perlobj.
28
29       "UNIVERSAL" provides the following methods:
30
31       "$obj->isa( TYPE )"
32       "CLASS->isa( TYPE )"
33       "eval { VAL->isa( TYPE ) }"
34           Where
35
36           "TYPE"
37               is a package name
38
39           $obj
40               is a blessed reference or a package name
41
42           "CLASS"
43               is a package name
44
45           "VAL"
46               is any of the above or an unblessed reference
47
48           When used as an instance or class method ("$obj->isa( TYPE )"),
49           "isa" returns true if $obj is blessed into package "TYPE" or
50           inherits from package "TYPE".
51
52           When used as a class method ("CLASS->isa( TYPE )", sometimes
53           referred to as a static method), "isa" returns true if "CLASS"
54           inherits from (or is itself) the name of the package "TYPE" or
55           inherits from package "TYPE".
56
57           If you're not sure what you have (the "VAL" case), wrap the method
58           call in an "eval" block to catch the exception if "VAL" is
59           undefined or an unblessed reference. The "isa" operator is an
60           alternative that simply returns false in this case, so the "eval"
61           is not needed.
62
63           If you want to be sure that you're calling "isa" as a method, not a
64           class, check the invocand with "blessed" from Scalar::Util first:
65
66             use Scalar::Util 'blessed';
67
68             if ( blessed( $obj ) && $obj->isa("Some::Class") ) {
69                 ...
70             }
71
72       "$obj->DOES( ROLE )"
73       "CLASS->DOES( ROLE )"
74           "DOES" checks if the object or class performs the role "ROLE".  A
75           role is a named group of specific behavior (often methods of
76           particular names and signatures), similar to a class, but not
77           necessarily a complete class by itself.  For example, logging or
78           serialization may be roles.
79
80           "DOES" and "isa" are similar, in that if either is true, you know
81           that the object or class on which you call the method can perform
82           specific behavior.  However, "DOES" is different from "isa" in that
83           it does not care how the invocand performs the operations, merely
84           that it does.  ("isa" of course mandates an inheritance
85           relationship.  Other relationships include aggregation, delegation,
86           and mocking.)
87
88           By default, classes in Perl only perform the "UNIVERSAL" role, as
89           well as the role of all classes in their inheritance.  In other
90           words, by default "DOES" responds identically to "isa".
91
92           There is a relationship between roles and classes, as each class
93           implies the existence of a role of the same name.  There is also a
94           relationship between inheritance and roles, in that a subclass that
95           inherits from an ancestor class implicitly performs any roles its
96           parent performs.  Thus you can use "DOES" in place of "isa" safely,
97           as it will return true in all places where "isa" will return true
98           (provided that any overridden "DOES" and "isa" methods behave
99           appropriately).
100
101       "$obj->can( METHOD )"
102       "CLASS->can( METHOD )"
103       "eval { VAL->can( METHOD ) }"
104           "can" checks if the object or class has a method called "METHOD".
105           If it does, then it returns a reference to the sub.  If it does
106           not, then it returns undef.  This includes methods inherited or
107           imported by $obj, "CLASS", or "VAL".
108
109           "can" cannot know whether an object will be able to provide a
110           method through AUTOLOAD (unless the object's class has overridden
111           "can" appropriately), so a return value of undef does not
112           necessarily mean the object will not be able to handle the method
113           call. To get around this some module authors use a forward
114           declaration (see perlsub) for methods they will handle via
115           AUTOLOAD. For such 'dummy' subs, "can" will still return a code
116           reference, which, when called, will fall through to the AUTOLOAD.
117           If no suitable AUTOLOAD is provided, calling the coderef will cause
118           an error.
119
120           You may call "can" as a class (static) method or an object method.
121
122           Again, the same rule about having a valid invocand applies -- use
123           an "eval" block or "blessed" if you need to be extra paranoid.
124
125       "VERSION ( [ REQUIRE ] )"
126           "VERSION" will return the value of the variable $VERSION in the
127           package the object is blessed into. If "REQUIRE" is given then it
128           will do a comparison and die if the package version is not greater
129           than or equal to "REQUIRE", or if either $VERSION or "REQUIRE" is
130           not a "lax" version number (as defined by the version module).
131
132           The return from "VERSION" will actually be the stringified version
133           object using the package $VERSION scalar, which is guaranteed to be
134           equivalent but may not be precisely the contents of the $VERSION
135           scalar.  If you want the actual contents of $VERSION, use
136           $CLASS::VERSION instead.
137
138           "VERSION" can be called as either a class (static) method or an
139           object method.
140

WARNINGS

142       NOTE: "can" directly uses Perl's internal code for method lookup, and
143       "isa" uses a very similar method and cache-ing strategy. This may cause
144       strange effects if the Perl code dynamically changes @ISA in any
145       package.
146
147       You may add other methods to the UNIVERSAL class via Perl or XS code.
148       You do not need to "use UNIVERSAL" to make these methods available to
149       your program (and you should not do so).
150

EXPORTS

152       None.
153
154       Previous versions of this documentation suggested using "isa" as a
155       function to determine the type of a reference:
156
157         $yes = UNIVERSAL::isa($h, "HASH");
158         $yes = UNIVERSAL::isa("Foo", "Bar");
159
160       The problem is that this code would never call an overridden "isa"
161       method in any class.  Instead, use "reftype" from Scalar::Util for the
162       first case:
163
164         use Scalar::Util 'reftype';
165
166         $yes = reftype( $h ) eq "HASH";
167
168       and the method form of "isa" for the second:
169
170         $yes = Foo->isa("Bar");
171
172
173
174perl v5.36.0                      2022-08-30                    UNIVERSAL(3pm)
Impressum