This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Update perlfunc/require to describe NXDOMAIN caching... er, you
[perl5.git] / lib / UNIVERSAL.pm
CommitLineData
def3c102
PP
1package UNIVERSAL;
2
2af1ab88 3our $VERSION = '1.01';
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
BS
7# accident that can't be fixed without breaking code. Note that we
8# *don't* set @ISA here, 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.
def3c102 11require Exporter;
84902520 12*import = \&Exporter::import;
ea8fae29 13@EXPORT_OK = qw(isa can VERSION);
def3c102
PP
14
151;
16__END__
17
18=head1 NAME
19
20UNIVERSAL - base class for ALL classes (blessed references)
21
22=head1 SYNOPSIS
23
ea8fae29
BS
24 $is_io = $fd->isa("IO::Handle");
25 $is_io = Class->isa("IO::Handle");
def3c102 26
ea8fae29
BS
27 $sub = $obj->can("print");
28 $sub = Class->can("print");
29
30 use UNIVERSAL qw( isa can VERSION );
31 $yes = isa $ref, "HASH" ;
32 $sub = can $ref, "fandango" ;
33 $ver = VERSION $obj ;
84902520 34
def3c102
PP
35=head1 DESCRIPTION
36
37C<UNIVERSAL> is the base class which all bless references will inherit from,
ea8fae29 38see L<perlobj>.
def3c102 39
ea8fae29 40C<UNIVERSAL> provides the following methods and functions:
def3c102
PP
41
42=over 4
43
a2b59c1f 44=item C<< $obj->isa( TYPE ) >>
ea8fae29 45
a2b59c1f 46=item C<< CLASS->isa( TYPE ) >>
ea8fae29 47
a2b59c1f 48=item C<isa( VAL, TYPE )>
ea8fae29 49
a2b59c1f
CW
50Where
51
52=over 4
53
54=item C<TYPE>
55
56is a package name
57
58=item C<$obj>
59
60is a blessed reference or a string containing a package name
61
62=item C<CLASS>
63
64is a package name
65
66=item C<VAL>
67
68is any of the above or an unblessed reference
69
70=back
71
72When used as an instance or class method (C<< $obj->isa( TYPE ) >>),
73C<isa> returns I<true> if $obj is blessed into package C<TYPE> or
74inherits from package C<TYPE>.
75
76When used as a class method (C<< CLASS->isa( TYPE ) >>: sometimes
77referred to as a static method), C<isa> returns I<true> if C<CLASS>
78inherits from (or is itself) the name of the package C<TYPE> or
79inherits from package C<TYPE>.
ea8fae29
BS
80
81When used as a function, like
82
83 use UNIVERSAL qw( isa ) ;
84 $yes = isa $h, "HASH";
85 $yes = isa "Foo", "Bar";
def3c102 86
ea8fae29 87or
def3c102 88
ea8fae29
BS
89 require UNIVERSAL ;
90 $yes = UNIVERSAL::isa $a, "ARRAY";
def3c102 91
a2b59c1f 92C<isa> returns I<true> in the same cases as above and also if C<VAL> is an
ea8fae29
BS
93unblessed reference to a perl variable of type C<TYPE>, such as "HASH",
94"ARRAY", or "Regexp".
def3c102 95
a2b59c1f
CW
96=item C<< $obj->can( METHOD ) >>
97
98=item C<< CLASS->can( METHOD ) >>
99
100=item C<can( VAL, METHOD )>
ea8fae29
BS
101
102C<can> checks if the object or class has a method called C<METHOD>. If it does
103then a reference to the sub is returned. If it does not then I<undef> is
104returned. This includes methods inherited or imported by C<$obj>, C<CLASS>, or
105C<VAL>.
def3c102 106
04b85669
TB
107C<can> cannot know whether an object will be able to provide a method
108through AUTOLOAD, so a return value of I<undef> does not necessarily mean
109the object will not be able to handle the method call. To get around
110this some module authors use a forward declaration (see L<perlsub>)
111for methods they will handle via AUTOLOAD. For such 'dummy' subs, C<can>
112will still return a code reference, which, when called, will fall through
113to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef
114will cause an error.
115
ea8fae29
BS
116C<can> can be called as a class (static) method, an object method, or a
117function.
118
119When used as a function, if C<VAL> is a blessed reference or package name which
120has a method called C<METHOD>, C<can> returns a reference to the subroutine.
121If C<VAL> is not a blessed reference, or if it does not have a method
122C<METHOD>, I<undef> is returned.
def3c102 123
a2b59c1f 124=item C<VERSION ( [ REQUIRE ] )>
def3c102
PP
125
126C<VERSION> will return the value of the variable C<$VERSION> in the
127package the object is blessed into. If C<REQUIRE> is given then
128it will do a comparison and die if the package version is not
129greater than or equal to C<REQUIRE>.
130
a2b59c1f
CW
131C<VERSION> can be called as either a class (static) method, an object
132method or a function.
a66bc3b0 133
a66bc3b0 134
def3c102
PP
135=back
136
a2b59c1f 137=head1 EXPORTS
84902520 138
a2b59c1f 139None by default.
84902520 140
a2b59c1f
CW
141You may request the import of all three functions (C<isa>, C<can>, and
142C<VERSION>), however it isn't usually necessary to do so. Perl magically
143makes these functions act as methods on all objects. The one exception is
144C<isa>, which is useful as a function when operating on non-blessed
145references.
84902520 146
def3c102 147=cut