| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "UNIVERSAL 3perl" |
| 58 |
.TH UNIVERSAL 3perl 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
UNIVERSAL \- base class for ALL classes (blessed references) |
| 65 |
.SH SYNOPSIS |
| 66 |
.IX Header "SYNOPSIS" |
| 67 |
.Vb 2 |
| 68 |
\& my $obj_is_io = $fd\->isa("IO::Handle"); |
| 69 |
\& my $cls_is_io = Class\->isa("IO::Handle"); |
| 70 |
\& |
| 71 |
\& my $obj_does_log = $obj\->DOES("Logger"); |
| 72 |
\& my $cls_does_log = Class\->DOES("Logger"); |
| 73 |
\& |
| 74 |
\& my $obj_sub = $obj\->can("print"); |
| 75 |
\& my $cls_sub = Class\->can("print"); |
| 76 |
\& |
| 77 |
\& my $eval_sub = eval { $ref\->can("fandango") }; |
| 78 |
\& my $ver = $obj\->VERSION; |
| 79 |
\& |
| 80 |
\& # but never do this! |
| 81 |
\& my $is_io = UNIVERSAL::isa($fd, "IO::Handle"); |
| 82 |
\& my $sub = UNIVERSAL::can($obj, "print"); |
| 83 |
.Ve |
| 84 |
.SH DESCRIPTION |
| 85 |
.IX Header "DESCRIPTION" |
| 86 |
\&\f(CW\*(C`UNIVERSAL\*(C'\fR is the base class from which all blessed references inherit. |
| 87 |
See perlobj. |
| 88 |
.PP |
| 89 |
\&\f(CW\*(C`UNIVERSAL\*(C'\fR provides the following methods: |
| 90 |
.ie n .IP """$obj\->isa( TYPE )""" 4 |
| 91 |
.el .IP "\f(CW$obj\->isa( TYPE )\fR" 4 |
| 92 |
.IX Item "$obj->isa( TYPE )" |
| 93 |
.PD 0 |
| 94 |
.ie n .IP """CLASS\->isa( TYPE )""" 4 |
| 95 |
.el .IP "\f(CWCLASS\->isa( TYPE )\fR" 4 |
| 96 |
.IX Item "CLASS->isa( TYPE )" |
| 97 |
.ie n .IP """eval { VAL\->isa( TYPE ) }""" 4 |
| 98 |
.el .IP "\f(CWeval { VAL\->isa( TYPE ) }\fR" 4 |
| 99 |
.IX Item "eval { VAL->isa( TYPE ) }" |
| 100 |
.PD |
| 101 |
Where |
| 102 |
.RS 4 |
| 103 |
.ie n .IP """TYPE""" 4 |
| 104 |
.el .IP \f(CWTYPE\fR 4 |
| 105 |
.IX Item "TYPE" |
| 106 |
is a package name |
| 107 |
.ie n .IP $obj 4 |
| 108 |
.el .IP \f(CW$obj\fR 4 |
| 109 |
.IX Item "$obj" |
| 110 |
is a blessed reference or a package name |
| 111 |
.ie n .IP """CLASS""" 4 |
| 112 |
.el .IP \f(CWCLASS\fR 4 |
| 113 |
.IX Item "CLASS" |
| 114 |
is a package name |
| 115 |
.ie n .IP """VAL""" 4 |
| 116 |
.el .IP \f(CWVAL\fR 4 |
| 117 |
.IX Item "VAL" |
| 118 |
is any of the above or an unblessed reference |
| 119 |
.RE |
| 120 |
.RS 4 |
| 121 |
.Sp |
| 122 |
When used as an instance or class method (\f(CW\*(C`$obj\->isa( TYPE )\*(C'\fR), |
| 123 |
\&\f(CW\*(C`isa\*(C'\fR returns \fItrue\fR if \f(CW$obj\fR is blessed into package \f(CW\*(C`TYPE\*(C'\fR or |
| 124 |
inherits from package \f(CW\*(C`TYPE\*(C'\fR. |
| 125 |
.Sp |
| 126 |
When used as a class method (\f(CW\*(C`CLASS\->isa( TYPE )\*(C'\fR, sometimes |
| 127 |
referred to as a static method), \f(CW\*(C`isa\*(C'\fR returns \fItrue\fR if \f(CW\*(C`CLASS\*(C'\fR |
| 128 |
inherits from (or is itself) the name of the package \f(CW\*(C`TYPE\*(C'\fR or |
| 129 |
inherits from package \f(CW\*(C`TYPE\*(C'\fR. |
| 130 |
.Sp |
| 131 |
If you're not sure what you have (the \f(CW\*(C`VAL\*(C'\fR case), wrap the method call in an |
| 132 |
\&\f(CW\*(C`eval\*(C'\fR block to catch the exception if \f(CW\*(C`VAL\*(C'\fR is undefined or an unblessed |
| 133 |
reference. The \f(CW\*(C`isa\*(C'\fR operator is an |
| 134 |
alternative that simply returns false in this case, so the \f(CW\*(C`eval\*(C'\fR is not |
| 135 |
needed. |
| 136 |
.Sp |
| 137 |
If you want to be sure that you're calling \f(CW\*(C`isa\*(C'\fR on an instance, not a class, |
| 138 |
check the invocant with \f(CW\*(C`blessed\*(C'\fR from Scalar::Util first: |
| 139 |
.Sp |
| 140 |
.Vb 1 |
| 141 |
\& use Scalar::Util \*(Aqblessed\*(Aq; |
| 142 |
\& |
| 143 |
\& if ( blessed( $obj ) && $obj\->isa("Some::Class") ) { |
| 144 |
\& ... |
| 145 |
\& } |
| 146 |
.Ve |
| 147 |
.RE |
| 148 |
.ie n .IP """$obj\->DOES( ROLE )""" 4 |
| 149 |
.el .IP "\f(CW$obj\->DOES( ROLE )\fR" 4 |
| 150 |
.IX Item "$obj->DOES( ROLE )" |
| 151 |
.PD 0 |
| 152 |
.ie n .IP """CLASS\->DOES( ROLE )""" 4 |
| 153 |
.el .IP "\f(CWCLASS\->DOES( ROLE )\fR" 4 |
| 154 |
.IX Item "CLASS->DOES( ROLE )" |
| 155 |
.PD |
| 156 |
\&\f(CW\*(C`DOES\*(C'\fR checks if the object or class performs the role \f(CW\*(C`ROLE\*(C'\fR. A role is a |
| 157 |
named group of specific behavior (often methods of particular names and |
| 158 |
signatures), similar to a class, but not necessarily a complete class by |
| 159 |
itself. For example, logging or serialization may be roles. |
| 160 |
.Sp |
| 161 |
\&\f(CW\*(C`DOES\*(C'\fR and \f(CW\*(C`isa\*(C'\fR are similar, in that if either is true, you know that the |
| 162 |
object or class on which you call the method can perform specific behavior. |
| 163 |
However, \f(CW\*(C`DOES\*(C'\fR is different from \f(CW\*(C`isa\*(C'\fR in that it does not care \fIhow\fR the |
| 164 |
invocand performs the operations, merely that it does. (\f(CW\*(C`isa\*(C'\fR of course |
| 165 |
mandates an inheritance relationship. Other relationships include aggregation, |
| 166 |
delegation, and mocking.) |
| 167 |
.Sp |
| 168 |
By default, classes in Perl only perform the \f(CW\*(C`UNIVERSAL\*(C'\fR role, as well as the |
| 169 |
role of all classes in their inheritance. In other words, by default \f(CW\*(C`DOES\*(C'\fR |
| 170 |
responds identically to \f(CW\*(C`isa\*(C'\fR. |
| 171 |
.Sp |
| 172 |
There is a relationship between roles and classes, as each class implies the |
| 173 |
existence of a role of the same name. There is also a relationship between |
| 174 |
inheritance and roles, in that a subclass that inherits from an ancestor class |
| 175 |
implicitly performs any roles its parent performs. Thus you can use \f(CW\*(C`DOES\*(C'\fR in |
| 176 |
place of \f(CW\*(C`isa\*(C'\fR safely, as it will return true in all places where \f(CW\*(C`isa\*(C'\fR will |
| 177 |
return true (provided that any overridden \f(CW\*(C`DOES\*(C'\fR \fIand\fR \f(CW\*(C`isa\*(C'\fR methods behave |
| 178 |
appropriately). |
| 179 |
.ie n .IP """$obj\->can( METHOD )""" 4 |
| 180 |
.el .IP "\f(CW$obj\->can( METHOD )\fR" 4 |
| 181 |
.IX Item "$obj->can( METHOD )" |
| 182 |
.PD 0 |
| 183 |
.ie n .IP """CLASS\->can( METHOD )""" 4 |
| 184 |
.el .IP "\f(CWCLASS\->can( METHOD )\fR" 4 |
| 185 |
.IX Item "CLASS->can( METHOD )" |
| 186 |
.ie n .IP """eval { VAL\->can( METHOD ) }""" 4 |
| 187 |
.el .IP "\f(CWeval { VAL\->can( METHOD ) }\fR" 4 |
| 188 |
.IX Item "eval { VAL->can( METHOD ) }" |
| 189 |
.PD |
| 190 |
\&\f(CW\*(C`can\*(C'\fR checks if the object or class has a method called \f(CW\*(C`METHOD\*(C'\fR. If it does, |
| 191 |
then it returns a reference to the sub. If it does not, then it returns |
| 192 |
\&\fIundef\fR. This includes methods inherited or imported by \f(CW$obj\fR, \f(CW\*(C`CLASS\*(C'\fR, or |
| 193 |
\&\f(CW\*(C`VAL\*(C'\fR. |
| 194 |
.Sp |
| 195 |
\&\f(CW\*(C`can\*(C'\fR cannot know whether an object will be able to provide a method through |
| 196 |
AUTOLOAD (unless the object's class has overridden \f(CW\*(C`can\*(C'\fR appropriately), so a |
| 197 |
return value of \fIundef\fR does not necessarily mean the object will not be able |
| 198 |
to handle the method call. To get around this some module authors use a forward |
| 199 |
declaration (see perlsub) for methods they will handle via AUTOLOAD. For |
| 200 |
such 'dummy' subs, \f(CW\*(C`can\*(C'\fR will still return a code reference, which, when |
| 201 |
called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, |
| 202 |
calling the coderef will cause an error. |
| 203 |
.Sp |
| 204 |
You may call \f(CW\*(C`can\*(C'\fR as a class (static) method or an object method. |
| 205 |
.Sp |
| 206 |
Again, the same rule about having a valid invocand applies \-\- use an \f(CW\*(C`eval\*(C'\fR |
| 207 |
block or \f(CW\*(C`blessed\*(C'\fR if you need to be extra paranoid. |
| 208 |
.ie n .IP """VERSION ( [ REQUIRE ] )""" 4 |
| 209 |
.el .IP "\f(CWVERSION ( [ REQUIRE ] )\fR" 4 |
| 210 |
.IX Item "VERSION ( [ REQUIRE ] )" |
| 211 |
\&\f(CW\*(C`VERSION\*(C'\fR will return the value of the variable \f(CW$VERSION\fR in the |
| 212 |
package the object is blessed into. If \f(CW\*(C`REQUIRE\*(C'\fR is given then |
| 213 |
it will do a comparison and die if the package version is not |
| 214 |
greater than or equal to \f(CW\*(C`REQUIRE\*(C'\fR, or if either \f(CW$VERSION\fR or \f(CW\*(C`REQUIRE\*(C'\fR |
| 215 |
is not a "lax" version number (as defined by the version module). |
| 216 |
.Sp |
| 217 |
The return from \f(CW\*(C`VERSION\*(C'\fR will actually be the stringified version object |
| 218 |
using the package \f(CW$VERSION\fR scalar, which is guaranteed to be equivalent |
| 219 |
but may not be precisely the contents of the \f(CW$VERSION\fR scalar. If you want |
| 220 |
the actual contents of \f(CW$VERSION\fR, use \f(CW$CLASS::VERSION\fR instead. |
| 221 |
.Sp |
| 222 |
\&\f(CW\*(C`VERSION\*(C'\fR can be called as either a class (static) method or an object |
| 223 |
method. |
| 224 |
.SH WARNINGS |
| 225 |
.IX Header "WARNINGS" |
| 226 |
\&\fBNOTE:\fR \f(CW\*(C`can\*(C'\fR directly uses Perl's internal code for method lookup, and |
| 227 |
\&\f(CW\*(C`isa\*(C'\fR uses a very similar method and cache-ing strategy. This may cause |
| 228 |
strange effects if the Perl code dynamically changes \f(CW@ISA\fR in any package. |
| 229 |
.PP |
| 230 |
You may add other methods to the UNIVERSAL class via Perl or XS code. |
| 231 |
You do not need to \f(CW\*(C`use UNIVERSAL\*(C'\fR to make these methods |
| 232 |
available to your program (and you should not do so). |
| 233 |
.SH EXPORTS |
| 234 |
.IX Header "EXPORTS" |
| 235 |
None. |
| 236 |
.PP |
| 237 |
Previous versions of this documentation suggested using \f(CW\*(C`isa\*(C'\fR as |
| 238 |
a function to determine the type of a reference: |
| 239 |
.PP |
| 240 |
.Vb 2 |
| 241 |
\& $yes = UNIVERSAL::isa($h, "HASH"); |
| 242 |
\& $yes = UNIVERSAL::isa("Foo", "Bar"); |
| 243 |
.Ve |
| 244 |
.PP |
| 245 |
The problem is that this code would \fInever\fR call an overridden \f(CW\*(C`isa\*(C'\fR method in |
| 246 |
any class. Instead, use \f(CW\*(C`reftype\*(C'\fR from Scalar::Util for the first case: |
| 247 |
.PP |
| 248 |
.Vb 1 |
| 249 |
\& use Scalar::Util \*(Aqreftype\*(Aq; |
| 250 |
\& |
| 251 |
\& $yes = reftype( $h ) eq "HASH"; |
| 252 |
.Ve |
| 253 |
.PP |
| 254 |
and the method form of \f(CW\*(C`isa\*(C'\fR for the second: |
| 255 |
.PP |
| 256 |
.Vb 1 |
| 257 |
\& $yes = Foo\->isa("Bar"); |
| 258 |
.Ve |