Commit | Line | Data |
---|---|---|
def3c102 | 1 | package UNIVERSAL; |
2 | ||
b75c8c73 MS |
3 | our $VERSION = '1.00'; |
4 | ||
84902520 TB |
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 | |
ea8fae29 BS |
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. | |
def3c102 | 11 | require Exporter; |
84902520 | 12 | *import = \&Exporter::import; |
ea8fae29 | 13 | @EXPORT_OK = qw(isa can VERSION); |
def3c102 | 14 | |
15 | 1; | |
16 | __END__ | |
17 | ||
18 | =head1 NAME | |
19 | ||
20 | UNIVERSAL - base class for ALL classes (blessed references) | |
21 | ||
22 | =head1 SYNOPSIS | |
23 | ||
ea8fae29 BS |
24 | $is_io = $fd->isa("IO::Handle"); |
25 | $is_io = Class->isa("IO::Handle"); | |
def3c102 | 26 | |
ea8fae29 BS |
27 | $sub = $obj->can("print"); |
28 | $sub = Class->can("print"); | |
29 | ||
30 | use UNIVERSAL qw( isa can VERSION ); | |
31 | $yes = isa $ref, "HASH" ; | |
32 | $sub = can $ref, "fandango" ; | |
33 | $ver = VERSION $obj ; | |
84902520 | 34 | |
def3c102 | 35 | =head1 DESCRIPTION |
36 | ||
37 | C<UNIVERSAL> is the base class which all bless references will inherit from, | |
ea8fae29 | 38 | see L<perlobj>. |
def3c102 | 39 | |
ea8fae29 | 40 | C<UNIVERSAL> provides the following methods and functions: |
def3c102 | 41 | |
42 | =over 4 | |
43 | ||
ea8fae29 BS |
44 | =item $obj->isa( TYPE ), CLASS->isa( TYPE ), isa( VAL, TYPE ) |
45 | ||
46 | C<TYPE> is a package name | |
47 | $obj is a blessed reference or a string containing a package name | |
48 | C<CLASS> is a package name | |
49 | C<VAL> is any of the above or an unblessed reference | |
50 | ||
51 | When used as an instance or class method (C<$obj->isa( TYPE )>), C<isa> | |
52 | returns I<true> if $obj is blessed into package C<TYPE> or inherits from | |
53 | package C<TYPE>. | |
54 | ||
55 | When used as a class method (C<CLASS->isa( TYPE )>; sometimes referred to as a | |
56 | static method), C<isa> returns I<true> if C<CLASS> inherits from (or is itself) | |
57 | the name of the package C<TYPE> or inherits from package C<TYPE>. | |
58 | ||
59 | When used as a function, like | |
60 | ||
61 | use UNIVERSAL qw( isa ) ; | |
62 | $yes = isa $h, "HASH"; | |
63 | $yes = isa "Foo", "Bar"; | |
def3c102 | 64 | |
ea8fae29 | 65 | or |
def3c102 | 66 | |
ea8fae29 BS |
67 | require UNIVERSAL ; |
68 | $yes = UNIVERSAL::isa $a, "ARRAY"; | |
def3c102 | 69 | |
ea8fae29 BS |
70 | , C<isa> returns I<true> in the same cases as above and also if C<VAL> is an |
71 | unblessed reference to a perl variable of type C<TYPE>, such as "HASH", | |
72 | "ARRAY", or "Regexp". | |
def3c102 | 73 | |
ea8fae29 BS |
74 | =item $obj->can( METHOD ), CLASS->can( METHOD ), can( VAL, METHOD ) |
75 | ||
76 | C<can> checks if the object or class has a method called C<METHOD>. If it does | |
77 | then a reference to the sub is returned. If it does not then I<undef> is | |
78 | returned. This includes methods inherited or imported by C<$obj>, C<CLASS>, or | |
79 | C<VAL>. | |
def3c102 | 80 | |
04b85669 TB |
81 | C<can> cannot know whether an object will be able to provide a method |
82 | through AUTOLOAD, so a return value of I<undef> does not necessarily mean | |
83 | the object will not be able to handle the method call. To get around | |
84 | this some module authors use a forward declaration (see L<perlsub>) | |
85 | for methods they will handle via AUTOLOAD. For such 'dummy' subs, C<can> | |
86 | will still return a code reference, which, when called, will fall through | |
87 | to the AUTOLOAD. If no suitable AUTOLOAD is provided, calling the coderef | |
88 | will cause an error. | |
89 | ||
ea8fae29 BS |
90 | C<can> can be called as a class (static) method, an object method, or a |
91 | function. | |
92 | ||
93 | When used as a function, if C<VAL> is a blessed reference or package name which | |
94 | has a method called C<METHOD>, C<can> returns a reference to the subroutine. | |
95 | If C<VAL> is not a blessed reference, or if it does not have a method | |
96 | C<METHOD>, I<undef> is returned. | |
def3c102 | 97 | |
98 | =item VERSION ( [ REQUIRE ] ) | |
99 | ||
100 | C<VERSION> will return the value of the variable C<$VERSION> in the | |
101 | package the object is blessed into. If C<REQUIRE> is given then | |
102 | it will do a comparison and die if the package version is not | |
103 | greater than or equal to C<REQUIRE>. | |
104 | ||
ea8fae29 BS |
105 | C<VERSION> can be called as either a class (static) method, an object method or |
106 | or a function. | |
a66bc3b0 | 107 | |
a66bc3b0 | 108 | |
def3c102 | 109 | =back |
110 | ||
84902520 TB |
111 | These subroutines should I<not> be imported via S<C<use UNIVERSAL qw(...)>>. |
112 | If you want simple local access to them you can do | |
113 | ||
114 | *isa = \&UNIVERSAL::isa; | |
115 | ||
116 | to import isa into your package. | |
117 | ||
def3c102 | 118 | =cut |