This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[REPATCH lib/UNIVERSAL.pm] Recommend Against Using Methods as Functions
[perl5.git] / lib / UNIVERSAL.pm
1 package UNIVERSAL;
2
3 our $VERSION = '1.03';
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, 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.
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 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     $sub   = eval { $ref->can("fandango") };
37     $ver   = $obj->VERSION;
38
39     # but never do this!
40     $is_io = UNIVERSAL::isa($fd, "IO::Handle");
41     $sub   = UNIVERSAL::can($obj, "print");
42
43 =head1 DESCRIPTION
44
45 C<UNIVERSAL> is the base class from which all blessed references inherit.
46 See L<perlobj>.
47
48 C<UNIVERSAL> provides the following methods:
49
50 =over 4
51
52 =item C<< $obj->isa( TYPE ) >>
53
54 =item C<< CLASS->isa( TYPE ) >>
55
56 =item C<< eval { VAL->isa( TYPE ) } >>
57
58 Where
59
60 =over 4
61
62 =item C<TYPE>
63
64 is a package name
65
66 =item C<$obj>
67
68 is a blessed reference or a string containing a package name
69
70 =item C<CLASS>
71
72 is a package name
73
74 =item C<VAL>
75
76 is any of the above or an unblessed reference
77
78 =back
79
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>.
83
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>.
88
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.
91
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:
94
95   use Scalar::Util 'blessed';
96
97   if ( blessed( $obj ) && $obj->isa("Some::Class") {
98       ...
99   }
100
101 =item C<< $obj->can( METHOD ) >>
102
103 =item C<< CLASS->can( METHOD ) >>
104
105 =item C<< eval { VAL->can( METHOD ) } >>
106
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
110 C<VAL>.
111
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.
120
121 You may call C<can> as a class (static) method or an object method.
122
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.
125
126 =item C<VERSION ( [ REQUIRE ] )>
127
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>.
132
133 C<VERSION> can be called as either a class (static) method or an object
134 method.
135
136 =back
137
138 =head1 EXPORTS
139
140 None by default.
141
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
144 new code.
145
146 For example, previous versions of this documentation suggested using C<isa> as
147 a function to determine the type of a reference:
148
149   use UNIVERSAL 'isa';
150
151   $yes = isa $h, "HASH";
152   $yes = isa "Foo", "Bar";
153
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:
156
157   use Scalar::Util 'reftype';
158
159   $yes = reftype( $h ) eq "HASH";
160
161 and the method form of C<isa> for the second:
162
163   $yes = Foo->isa("Bar");
164
165 =cut