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