This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
don't depend on threads to do a watchdog when testing threads
[perl5.git] / lib / UNIVERSAL.pm
CommitLineData
def3c102 1package UNIVERSAL;
2
e2af7601 3our $VERSION = '1.10';
b75c8c73 4
84902520
TB
5# UNIVERSAL should not contain any extra subs/methods beyond those
6# that it exists to define. The use of Exporter below is a historical
ea8fae29 7# accident that can't be fixed without breaking code. Note that we
71ccbdc2 8# *don't* set @ISA here, as we don't want all classes/objects inheriting from
ea8fae29
BS
9# Exporter. It's bad enough that all classes have a import() method
10# whenever UNIVERSAL.pm is loaded.
def3c102 11require Exporter;
ea8fae29 12@EXPORT_OK = qw(isa can VERSION);
def3c102 13
2bfd5681 14# Make sure that even though the import method is called, it doesn't do
71ccbdc2 15# anything unless called on UNIVERSAL.
2bfd5681
MS
16sub import {
17 return unless $_[0] eq __PACKAGE__;
252143cd 18 return unless @_ > 1;
b3f1e0ca 19 require warnings;
1d9f57de
RS
20 warnings::warnif(
21 'deprecated',
22 'UNIVERSAL->import is deprecated and will be removed in a future perl',
23 );
2bfd5681
MS
24 goto &Exporter::import;
25}
26
def3c102 271;
28__END__
29
30=head1 NAME
31
32UNIVERSAL - base class for ALL classes (blessed references)
33
34=head1 SYNOPSIS
35
7d1bbbe8 36 $is_io = $fd->isa("IO::Handle");
37 $is_io = Class->isa("IO::Handle");
def3c102 38
7d1bbbe8 39 $does_log = $obj->DOES("Logger");
40 $does_log = Class->DOES("Logger");
ea8fae29 41
7d1bbbe8 42 $sub = $obj->can("print");
43 $sub = Class->can("print");
44
45 $sub = eval { $ref->can("fandango") };
46 $ver = $obj->VERSION;
71ccbdc2 47
48 # but never do this!
7d1bbbe8 49 $is_io = UNIVERSAL::isa($fd, "IO::Handle");
50 $sub = UNIVERSAL::can($obj, "print");
84902520 51
def3c102 52=head1 DESCRIPTION
53
71ccbdc2 54C<UNIVERSAL> is the base class from which all blessed references inherit.
55See L<perlobj>.
def3c102 56
71ccbdc2 57C<UNIVERSAL> provides the following methods:
def3c102 58
59=over 4
60
a2b59c1f 61=item C<< $obj->isa( TYPE ) >>
ea8fae29 62
71ccbdc2 63=item C<< CLASS->isa( TYPE ) >>
ea8fae29 64
71ccbdc2 65=item C<< eval { VAL->isa( TYPE ) } >>
ea8fae29 66
a2b59c1f
CW
67Where
68
69=over 4
70
71=item C<TYPE>
72
73is a package name
74
75=item C<$obj>
76
003db2bd 77is a blessed reference or a package name
a2b59c1f
CW
78
79=item C<CLASS>
80
81is a package name
82
83=item C<VAL>
84
85is any of the above or an unblessed reference
86
87=back
88
89When used as an instance or class method (C<< $obj->isa( TYPE ) >>),
90C<isa> returns I<true> if $obj is blessed into package C<TYPE> or
91inherits from package C<TYPE>.
92
71ccbdc2 93When used as a class method (C<< CLASS->isa( TYPE ) >>, sometimes
a2b59c1f
CW
94referred to as a static method), C<isa> returns I<true> if C<CLASS>
95inherits from (or is itself) the name of the package C<TYPE> or
96inherits from package C<TYPE>.
ea8fae29 97
71ccbdc2 98If you're not sure what you have (the C<VAL> case), wrap the method call in an
99C<eval> block to catch the exception if C<VAL> is undefined.
def3c102 100
71ccbdc2 101If you want to be sure that you're calling C<isa> as a method, not a class,
98dc9551 102check the invocand with C<blessed> from L<Scalar::Util> first:
def3c102 103
71ccbdc2 104 use Scalar::Util 'blessed';
def3c102 105
e2af7601 106 if ( blessed( $obj ) && $obj->isa("Some::Class") ) {
71ccbdc2 107 ...
108 }
def3c102 109
7d1bbbe8 110=item C<< $obj->DOES( ROLE ) >>
111
112=item C<< CLASS->DOES( ROLE ) >>
113
114C<DOES> checks if the object or class performs the role C<ROLE>. A role is a
115named group of specific behavior (often methods of particular names and
116signatures), similar to a class, but not necessarily a complete class by
117itself. For example, logging or serialization may be roles.
118
119C<DOES> and C<isa> are similar, in that if either is true, you know that the
120object or class on which you call the method can perform specific behavior.
121However, C<DOES> is different from C<isa> in that it does not care I<how> the
98dc9551 122invocand performs the operations, merely that it does. (C<isa> of course
7d1bbbe8 123mandates an inheritance relationship. Other relationships include aggregation,
124delegation, and mocking.)
125
bcb8f0e8
RS
126By default, classes in Perl only perform the C<UNIVERSAL> role, as well as the
127role of all classes in their inheritance. In other words, by default C<DOES>
128responds identically to C<isa>.
7d1bbbe8 129
130There is a relationship between roles and classes, as each class implies the
131existence of a role of the same name. There is also a relationship between
132inheritance and roles, in that a subclass that inherits from an ancestor class
133implicitly performs any roles its parent performs. Thus you can use C<DOES> in
134place of C<isa> safely, as it will return true in all places where C<isa> will
135return true (provided that any overridden C<DOES> I<and> C<isa> methods behave
136appropriately).
137
a2b59c1f
CW
138=item C<< $obj->can( METHOD ) >>
139
140=item C<< CLASS->can( METHOD ) >>
141
71ccbdc2 142=item C<< eval { VAL->can( METHOD ) } >>
ea8fae29 143
71ccbdc2 144C<can> checks if the object or class has a method called C<METHOD>. If it does,
145then it returns a reference to the sub. If it does not, then it returns
146I<undef>. This includes methods inherited or imported by C<$obj>, C<CLASS>, or
ea8fae29 147C<VAL>.
def3c102 148
71ccbdc2 149C<can> cannot know whether an object will be able to provide a method through
fb78fdcd 150AUTOLOAD (unless the object's class has overridden C<can> appropriately), so a
71ccbdc2 151return value of I<undef> does not necessarily mean the object will not be able
152to handle the method call. To get around this some module authors use a forward
153declaration (see L<perlsub>) for methods they will handle via AUTOLOAD. For
154such 'dummy' subs, C<can> will still return a code reference, which, when
155called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided,
156calling the coderef will cause an error.
04b85669 157
71ccbdc2 158You may call C<can> as a class (static) method or an object method.
ea8fae29 159
98dc9551 160Again, the same rule about having a valid invocand applies -- use an C<eval>
71ccbdc2 161block or C<blessed> if you need to be extra paranoid.
def3c102 162
a2b59c1f 163=item C<VERSION ( [ REQUIRE ] )>
def3c102 164
165C<VERSION> will return the value of the variable C<$VERSION> in the
166package the object is blessed into. If C<REQUIRE> is given then
167it will do a comparison and die if the package version is not
e937c40d
FC
168greater than or equal to C<REQUIRE>, or if either C<$VERSION> or C<REQUIRE>
169is not a "lax" version number (as defined by the L<version> module).
def3c102 170
71ccbdc2 171C<VERSION> can be called as either a class (static) method or an object
172method.
a66bc3b0 173
def3c102 174=back
175
ba593fa9
RS
176=head1 WARNINGS
177
178B<NOTE:> C<can> directly uses Perl's internal code for method lookup, and
179C<isa> uses a very similar method and cache-ing strategy. This may cause
180strange effects if the Perl code dynamically changes @ISA in any package.
181
182You may add other methods to the UNIVERSAL class via Perl or XS code.
183You do not need to C<use UNIVERSAL> to make these methods
184available to your program (and you should not do so).
185
a2b59c1f 186=head1 EXPORTS
84902520 187
a2b59c1f 188None by default.
84902520 189
7d1bbbe8 190You may request the import of three functions (C<isa>, C<can>, and C<VERSION>),
1d9f57de
RS
191B<but this feature is deprecated and will be removed>. Please don't do this in
192new code.
71ccbdc2 193
194For example, previous versions of this documentation suggested using C<isa> as
195a function to determine the type of a reference:
196
197 use UNIVERSAL 'isa';
198
199 $yes = isa $h, "HASH";
200 $yes = isa "Foo", "Bar";
201
202The problem is that this code will I<never> call an overridden C<isa> method in
203any class. Instead, use C<reftype> from L<Scalar::Util> for the first case:
204
205 use Scalar::Util 'reftype';
206
207 $yes = reftype( $h ) eq "HASH";
208
209and the method form of C<isa> for the second:
210
211 $yes = Foo->isa("Bar");
84902520 212
def3c102 213=cut