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.
12 @EXPORT_OK = qw(isa can VERSION);
14 # Make sure that even though the import method is called, it doesn't do
15 # anything unless its called on UNIVERSAL
17 return unless $_[0] eq __PACKAGE__;
18 goto &Exporter::import;
26 UNIVERSAL - base class for ALL classes (blessed references)
30 $is_io = $fd->isa("IO::Handle");
31 $is_io = Class->isa("IO::Handle");
33 $sub = $obj->can("print");
34 $sub = Class->can("print");
36 use UNIVERSAL qw( isa can VERSION );
37 $yes = isa $ref, "HASH" ;
38 $sub = can $ref, "fandango" ;
43 C<UNIVERSAL> is the base class which all bless references will inherit from,
46 C<UNIVERSAL> provides the following methods and functions:
50 =item C<< $obj->isa( TYPE ) >>
52 =item C<< CLASS->isa( TYPE ) >>
54 =item C<isa( VAL, TYPE )>
66 is a blessed reference or a string containing a package name
74 is any of the above or an unblessed reference
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>.
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>.
87 When used as a function, like
89 use UNIVERSAL qw( isa ) ;
90 $yes = isa $h, "HASH";
91 $yes = isa "Foo", "Bar";
96 $yes = UNIVERSAL::isa $a, "ARRAY";
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".
102 =item C<< $obj->can( METHOD ) >>
104 =item C<< CLASS->can( METHOD ) >>
106 =item C<can( VAL, METHOD )>
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
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
122 C<can> can be called as a class (static) method, an object method, or a
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.
130 =item C<VERSION ( [ REQUIRE ] )>
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>.
137 C<VERSION> can be called as either a class (static) method, an object
138 method or a function.
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