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