1UNIVERSAL(3pm)         Perl Programmers Reference Guide         UNIVERSAL(3pm)
2
3
4

NAME

6       UNIVERSAL - base class for ALL classes (blessed references)
7

SYNOPSIS

9           $is_io    = $fd->isa("IO::Handle");
10           $is_io    = Class->isa("IO::Handle");
11
12           $does_log = $obj->DOES("Logger");
13           $does_log = Class->DOES("Logger");
14
15           $sub      = $obj->can("print");
16           $sub      = Class->can("print");
17
18           $sub      = eval { $ref->can("fandango") };
19           $ver      = $obj->VERSION;
20
21           # but never do this!
22           $is_io    = UNIVERSAL::isa($fd, "IO::Handle");
23           $sub      = UNIVERSAL::can($obj, "print");
24

DESCRIPTION

26       "UNIVERSAL" is the base class from which all blessed references
27       inherit.  See perlobj.
28
29       "UNIVERSAL" provides the following methods:
30
31       "$obj->isa( TYPE )"
32       "CLASS->isa( TYPE )"
33       "eval { VAL->isa( TYPE ) }"
34           Where
35
36           "TYPE"
37               is a package name
38
39           $obj
40               is a blessed reference or a package name
41
42           "CLASS"
43               is a package name
44
45           "VAL"
46               is any of the above or an unblessed reference
47
48           When used as an instance or class method ("$obj->isa( TYPE )"),
49           "isa" returns true if $obj is blessed into package "TYPE" or
50           inherits from package "TYPE".
51
52           When used as a class method ("CLASS->isa( TYPE )", sometimes
53           referred to as a static method), "isa" returns true if "CLASS"
54           inherits from (or is itself) the name of the package "TYPE" or
55           inherits from package "TYPE".
56
57           If you're not sure what you have (the "VAL" case), wrap the method
58           call in an "eval" block to catch the exception if "VAL" is
59           undefined.
60
61           If you want to be sure that you're calling "isa" as a method, not a
62           class, check the invocant with "blessed" from Scalar::Util first:
63
64             use Scalar::Util 'blessed';
65
66             if ( blessed( $obj ) && $obj->isa("Some::Class") {
67                 ...
68             }
69
70       "$obj->DOES( ROLE )"
71       "CLASS->DOES( ROLE )"
72           "DOES" checks if the object or class performs the role "ROLE".  A
73           role is a named group of specific behavior (often methods of
74           particular names and signatures), similar to a class, but not
75           necessarily a complete class by itself.  For example, logging or
76           serialization may be roles.
77
78           "DOES" and "isa" are similar, in that if either is true, you know
79           that the object or class on which you call the method can perform
80           specific behavior.  However, "DOES" is different from "isa" in that
81           it does not care how the invocant performs the operations, merely
82           that it does.  ("isa" of course mandates an inheritance
83           relationship.  Other relationships include aggregation, delegation,
84           and mocking.)
85
86           By default, classes in Perl only perform the "UNIVERSAL" role, as
87           well as the role of all classes in their inheritance.  In other
88           words, by default "DOES" responds identically to "isa".
89
90           There is a relationship between roles and classes, as each class
91           implies the existence of a role of the same name.  There is also a
92           relationship between inheritance and roles, in that a subclass that
93           inherits from an ancestor class implicitly performs any roles its
94           parent performs.  Thus you can use "DOES" in place of "isa" safely,
95           as it will return true in all places where "isa" will return true
96           (provided that any overridden "DOES" and "isa" methods behave
97           appropriately).
98
99       "$obj->can( METHOD )"
100       "CLASS->can( METHOD )"
101       "eval { VAL->can( METHOD ) }"
102           "can" checks if the object or class has a method called "METHOD".
103           If it does, then it returns a reference to the sub.  If it does
104           not, then it returns undef.  This includes methods inherited or
105           imported by $obj, "CLASS", or "VAL".
106
107           "can" cannot know whether an object will be able to provide a
108           method through AUTOLOAD (unless the object's class has overriden
109           "can" appropriately), so a return value of undef does not
110           necessarily mean the object will not be able to handle the method
111           call. To get around this some module authors use a forward
112           declaration (see perlsub) for methods they will handle via
113           AUTOLOAD. For such 'dummy' subs, "can" will still return a code
114           reference, which, when called, will fall through to the AUTOLOAD.
115           If no suitable AUTOLOAD is provided, calling the coderef will cause
116           an error.
117
118           You may call "can" as a class (static) method or an object method.
119
120           Again, the same rule about having a valid invocant applies -- use
121           an "eval" block or "blessed" if you need to be extra paranoid.
122
123       "VERSION ( [ REQUIRE ] )"
124           "VERSION" will return the value of the variable $VERSION in the
125           package the object is blessed into. If "REQUIRE" is given then it
126           will do a comparison and die if the package version is not greater
127           than or equal to "REQUIRE".
128
129           "VERSION" can be called as either a class (static) method or an
130           object method.
131

WARNINGS

133       NOTE: "can" directly uses Perl's internal code for method lookup, and
134       "isa" uses a very similar method and cache-ing strategy. This may cause
135       strange effects if the Perl code dynamically changes @ISA in any
136       package.
137
138       You may add other methods to the UNIVERSAL class via Perl or XS code.
139       You do not need to "use UNIVERSAL" to make these methods available to
140       your program (and you should not do so).
141

EXPORTS

143       None by default.
144
145       You may request the import of three functions ("isa", "can", and
146       "VERSION"), however it is usually harmful to do so.  Please don't do
147       this in new code.
148
149       For example, previous versions of this documentation suggested using
150       "isa" as a function to determine the type of a reference:
151
152         use UNIVERSAL 'isa';
153
154         $yes = isa $h, "HASH";
155         $yes = isa "Foo", "Bar";
156
157       The problem is that this code will never call an overridden "isa"
158       method in any class.  Instead, use "reftype" from Scalar::Util for the
159       first case:
160
161         use Scalar::Util 'reftype';
162
163         $yes = reftype( $h ) eq "HASH";
164
165       and the method form of "isa" for the second:
166
167         $yes = Foo->isa("Bar");
168
169
170
171perl v5.10.1                      2009-07-03                    UNIVERSAL(3pm)
Impressum