This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Comply with the 0x80th commandment
[perl5.git] / lib / UNIVERSAL.pm
CommitLineData
def3c102 1package UNIVERSAL;
2
71ccbdc2 3our $VERSION = '1.03';
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__;
18 goto &Exporter::import;
19}
20
def3c102 211;
22__END__
23
24=head1 NAME
25
26UNIVERSAL - base class for ALL classes (blessed references)
27
28=head1 SYNOPSIS
29
ea8fae29
BS
30 $is_io = $fd->isa("IO::Handle");
31 $is_io = Class->isa("IO::Handle");
def3c102 32
71ccbdc2 33 $sub = $obj->can("print");
34 $sub = Class->can("print");
ea8fae29 35
71ccbdc2 36 $sub = eval { $ref->can("fandango") };
37 $ver = $obj->VERSION;
38
39 # but never do this!
40 $is_io = UNIVERSAL::isa($fd, "IO::Handle");
41 $sub = UNIVERSAL::can($obj, "print");
84902520 42
def3c102 43=head1 DESCRIPTION
44
71ccbdc2 45C<UNIVERSAL> is the base class from which all blessed references inherit.
46See L<perlobj>.
def3c102 47
71ccbdc2 48C<UNIVERSAL> provides the following methods:
def3c102 49
50=over 4
51
a2b59c1f 52=item C<< $obj->isa( TYPE ) >>
ea8fae29 53
71ccbdc2 54=item C<< CLASS->isa( TYPE ) >>
ea8fae29 55
71ccbdc2 56=item C<< eval { VAL->isa( TYPE ) } >>
ea8fae29 57
a2b59c1f
CW
58Where
59
60=over 4
61
62=item C<TYPE>
63
64is a package name
65
66=item C<$obj>
67
68is a blessed reference or a string containing a package name
69
70=item C<CLASS>
71
72is a package name
73
74=item C<VAL>
75
76is any of the above or an unblessed reference
77
78=back
79
80When used as an instance or class method (C<< $obj->isa( TYPE ) >>),
81C<isa> returns I<true> if $obj is blessed into package C<TYPE> or
82inherits from package C<TYPE>.
83
71ccbdc2 84When used as a class method (C<< CLASS->isa( TYPE ) >>, sometimes
a2b59c1f
CW
85referred to as a static method), C<isa> returns I<true> if C<CLASS>
86inherits from (or is itself) the name of the package C<TYPE> or
87inherits from package C<TYPE>.
ea8fae29 88
71ccbdc2 89If you're not sure what you have (the C<VAL> case), wrap the method call in an
90C<eval> block to catch the exception if C<VAL> is undefined.
def3c102 91
71ccbdc2 92If you want to be sure that you're calling C<isa> as a method, not a class,
93check the invocant with C<blessed> from L<Scalar::Util> first:
def3c102 94
71ccbdc2 95 use Scalar::Util 'blessed';
def3c102 96
71ccbdc2 97 if ( blessed( $obj ) && $obj->isa("Some::Class") {
98 ...
99 }
def3c102 100
a2b59c1f
CW
101=item C<< $obj->can( METHOD ) >>
102
103=item C<< CLASS->can( METHOD ) >>
104
71ccbdc2 105=item C<< eval { VAL->can( METHOD ) } >>
ea8fae29 106
71ccbdc2 107C<can> checks if the object or class has a method called C<METHOD>. If it does,
108then it returns a reference to the sub. If it does not, then it returns
109I<undef>. This includes methods inherited or imported by C<$obj>, C<CLASS>, or
ea8fae29 110C<VAL>.
def3c102 111
71ccbdc2 112C<can> cannot know whether an object will be able to provide a method through
113AUTOLOAD (unless the object's class has overriden C<can> appropriately), so a
114return value of I<undef> does not necessarily mean the object will not be able
115to handle the method call. To get around this some module authors use a forward
116declaration (see L<perlsub>) for methods they will handle via AUTOLOAD. For
117such 'dummy' subs, C<can> will still return a code reference, which, when
118called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided,
119calling the coderef will cause an error.
04b85669 120
71ccbdc2 121You may call C<can> as a class (static) method or an object method.
ea8fae29 122
71ccbdc2 123Again, the same rule about having a valid invocant applies -- use an C<eval>
124block or C<blessed> if you need to be extra paranoid.
def3c102 125
a2b59c1f 126=item C<VERSION ( [ REQUIRE ] )>
def3c102 127
128C<VERSION> will return the value of the variable C<$VERSION> in the
129package the object is blessed into. If C<REQUIRE> is given then
130it will do a comparison and die if the package version is not
131greater than or equal to C<REQUIRE>.
132
71ccbdc2 133C<VERSION> can be called as either a class (static) method or an object
134method.
a66bc3b0 135
def3c102 136=back
137
a2b59c1f 138=head1 EXPORTS
84902520 139
a2b59c1f 140None by default.
84902520 141
a2b59c1f 142You may request the import of all three functions (C<isa>, C<can>, and
71ccbdc2 143C<VERSION>), however it is usually harmful to do so. Please don't do this in
144new code.
145
146For example, previous versions of this documentation suggested using C<isa> as
147a function to determine the type of a reference:
148
149 use UNIVERSAL 'isa';
150
151 $yes = isa $h, "HASH";
152 $yes = isa "Foo", "Bar";
153
154The problem is that this code will I<never> call an overridden C<isa> method in
155any class. Instead, use C<reftype> from L<Scalar::Util> for the first case:
156
157 use Scalar::Util 'reftype';
158
159 $yes = reftype( $h ) eq "HASH";
160
161and the method form of C<isa> for the second:
162
163 $yes = Foo->isa("Bar");
84902520 164
def3c102 165=cut