Updated to Git v1.8.4
[msysgit.git] / lib / perl5 / 5.8.8 / UNIVERSAL.pm
blob7b7bfc4058a40a6bbbfbfd70d47ab3105622c227
1 package UNIVERSAL;
3 our $VERSION = '1.01';
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 *import = \&Exporter::import;
13 @EXPORT_OK = qw(isa can VERSION);
16 __END__
18 =head1 NAME
20 UNIVERSAL - base class for ALL classes (blessed references)
22 =head1 SYNOPSIS
24 $is_io = $fd->isa("IO::Handle");
25 $is_io = Class->isa("IO::Handle");
27 $sub = $obj->can("print");
28 $sub = Class->can("print");
30 use UNIVERSAL qw( isa can VERSION );
31 $yes = isa $ref, "HASH" ;
32 $sub = can $ref, "fandango" ;
33 $ver = VERSION $obj ;
35 =head1 DESCRIPTION
37 C<UNIVERSAL> is the base class which all bless references will inherit from,
38 see L<perlobj>.
40 C<UNIVERSAL> provides the following methods and functions:
42 =over 4
44 =item C<< $obj->isa( TYPE ) >>
46 =item C<< CLASS->isa( TYPE ) >>
48 =item C<isa( VAL, TYPE )>
50 Where
52 =over 4
54 =item C<TYPE>
56 is a package name
58 =item C<$obj>
60 is a blessed reference or a string containing a package name
62 =item C<CLASS>
64 is a package name
66 =item C<VAL>
68 is any of the above or an unblessed reference
70 =back
72 When used as an instance or class method (C<< $obj->isa( TYPE ) >>),
73 C<isa> returns I<true> if $obj is blessed into package C<TYPE> or
74 inherits from package C<TYPE>.
76 When used as a class method (C<< CLASS->isa( TYPE ) >>: sometimes
77 referred to as a static method), C<isa> returns I<true> if C<CLASS>
78 inherits from (or is itself) the name of the package C<TYPE> or
79 inherits from package C<TYPE>.
81 When used as a function, like
83 use UNIVERSAL qw( isa ) ;
84 $yes = isa $h, "HASH";
85 $yes = isa "Foo", "Bar";
89 require UNIVERSAL ;
90 $yes = UNIVERSAL::isa $a, "ARRAY";
92 C<isa> returns I<true> in the same cases as above and also if C<VAL> is an
93 unblessed reference to a perl variable of type C<TYPE>, such as "HASH",
94 "ARRAY", or "Regexp".
96 =item C<< $obj->can( METHOD ) >>
98 =item C<< CLASS->can( METHOD ) >>
100 =item C<can( VAL, METHOD )>
102 C<can> checks if the object or class has a method called C<METHOD>. If it does
103 then a reference to the sub is returned. If it does not then I<undef> is
104 returned. This includes methods inherited or imported by C<$obj>, C<CLASS>, or
105 C<VAL>.
107 C<can> cannot know whether an object will be able to provide a method
108 through AUTOLOAD, so a return value of I<undef> does not necessarily mean
109 the object will not be able to handle the method call. To get around
110 this some module authors use a forward declaration (see L<perlsub>)
111 for methods they will handle via AUTOLOAD. For such 'dummy' subs, C<can>
112 will still return a code reference, which, when called, will fall through
113 to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef
114 will cause an error.
116 C<can> can be called as a class (static) method, an object method, or a
117 function.
119 When used as a function, if C<VAL> is a blessed reference or package name which
120 has a method called C<METHOD>, C<can> returns a reference to the subroutine.
121 If C<VAL> is not a blessed reference, or if it does not have a method
122 C<METHOD>, I<undef> is returned.
124 =item C<VERSION ( [ REQUIRE ] )>
126 C<VERSION> will return the value of the variable C<$VERSION> in the
127 package the object is blessed into. If C<REQUIRE> is given then
128 it will do a comparison and die if the package version is not
129 greater than or equal to C<REQUIRE>.
131 C<VERSION> can be called as either a class (static) method, an object
132 method or a function.
135 =back
137 =head1 EXPORTS
139 None by default.
141 You may request the import of all three functions (C<isa>, C<can>, and
142 C<VERSION>), however it isn't usually necessary to do so. Perl magically
143 makes these functions act as methods on all objects. The one exception is
144 C<isa>, which is useful as a function when operating on non-blessed
145 references.
147 =cut