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, as we 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 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 $sub = eval { $ref->can("fandango") };
40 $is_io = UNIVERSAL::isa($fd, "IO::Handle");
41 $sub = UNIVERSAL::can($obj, "print");
45 C<UNIVERSAL> is the base class from which all blessed references inherit.
48 C<UNIVERSAL> provides the following methods:
52 =item C<< $obj->isa( TYPE ) >>
54 =item C<< CLASS->isa( TYPE ) >>
56 =item C<< eval { VAL->isa( TYPE ) } >>
68 is a blessed reference or a string containing a package name
76 is any of the above or an unblessed reference
80 When used as an instance or class method (C<< $obj->isa( TYPE ) >>),
81 C<isa> returns I<true> if $obj is blessed into package C<TYPE> or
82 inherits from package C<TYPE>.
84 When used as a class method (C<< CLASS->isa( TYPE ) >>, sometimes
85 referred to as a static method), C<isa> returns I<true> if C<CLASS>
86 inherits from (or is itself) the name of the package C<TYPE> or
87 inherits from package C<TYPE>.
89 If you're not sure what you have (the C<VAL> case), wrap the method call in an
90 C<eval> block to catch the exception if C<VAL> is undefined.
92 If you want to be sure that you're calling C<isa> as a method, not a class,
93 check the invocant with C<blessed> from L<Scalar::Util> first:
95 use Scalar::Util 'blessed';
97 if ( blessed( $obj ) && $obj->isa("Some::Class") {
101 =item C<< $obj->can( METHOD ) >>
103 =item C<< CLASS->can( METHOD ) >>
105 =item C<< eval { VAL->can( METHOD ) } >>
107 C<can> checks if the object or class has a method called C<METHOD>. If it does,
108 then it returns a reference to the sub. If it does not, then it returns
109 I<undef>. This includes methods inherited or imported by C<$obj>, C<CLASS>, or
112 C<can> cannot know whether an object will be able to provide a method through
113 AUTOLOAD (unless the object's class has overriden C<can> appropriately), so a
114 return value of I<undef> does not necessarily mean the object will not be able
115 to handle the method call. To get around this some module authors use a forward
116 declaration (see L<perlsub>) for methods they will handle via AUTOLOAD. For
117 such 'dummy' subs, C<can> will still return a code reference, which, when
118 called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided,
119 calling the coderef will cause an error.
121 You may call C<can> as a class (static) method or an object method.
123 Again, the same rule about having a valid invocant applies -- use an C<eval>
124 block or C<blessed> if you need to be extra paranoid.
126 =item C<VERSION ( [ REQUIRE ] )>
128 C<VERSION> will return the value of the variable C<$VERSION> in the
129 package the object is blessed into. If C<REQUIRE> is given then
130 it will do a comparison and die if the package version is not
131 greater than or equal to C<REQUIRE>.
133 C<VERSION> can be called as either a class (static) method or an object
142 You may request the import of all three functions (C<isa>, C<can>, and
143 C<VERSION>), however it is usually harmful to do so. Please don't do this in
146 For example, previous versions of this documentation suggested using C<isa> as
147 a function to determine the type of a reference:
151 $yes = isa $h, "HASH";
152 $yes = isa "Foo", "Bar";
154 The problem is that this code will I<never> call an overridden C<isa> method in
155 any class. Instead, use C<reftype> from L<Scalar::Util> for the first case:
157 use Scalar::Util 'reftype';
159 $yes = reftype( $h ) eq "HASH";
161 and the method form of C<isa> for the second:
163 $yes = Foo->isa("Bar");