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

WARNINGS

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

EXPORTS

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