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