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