1UNIVERSAL(3pm) Perl Programmers Reference Guide UNIVERSAL(3pm)
2
3
4
6 UNIVERSAL - base class for ALL classes (blessed references)
7
9 $is_io = $fd->isa("IO::Handle");
10 $is_io = Class->isa("IO::Handle");
11
12 $sub = $obj->can("print");
13 $sub = Class->can("print");
14
15 use UNIVERSAL qw( isa can VERSION );
16 $yes = isa $ref, "HASH" ;
17 $sub = can $ref, "fandango" ;
18 $ver = VERSION $obj ;
19
21 "UNIVERSAL" is the base class which all bless references will inherit
22 from, see perlobj.
23
24 "UNIVERSAL" provides the following methods and functions:
25
26 "$obj->isa( TYPE )"
27 "CLASS->isa( TYPE )"
28 "isa( VAL, TYPE )"
29 Where
30
31 "TYPE"
32 is a package name
33
34 $obj
35 is a blessed reference or a string containing a package name
36
37 "CLASS"
38 is a package name
39
40 "VAL"
41 is any of the above or an unblessed reference
42
43 When used as an instance or class method ("$obj->isa( TYPE )"),
44 "isa" returns true if $obj is blessed into package "TYPE" or inher‐
45 its from package "TYPE".
46
47 When used as a class method ("CLASS->isa( TYPE )": sometimes
48 referred to as a static method), "isa" returns true if "CLASS"
49 inherits from (or is itself) the name of the package "TYPE" or
50 inherits from package "TYPE".
51
52 When used as a function, like
53
54 use UNIVERSAL qw( isa ) ;
55 $yes = isa $h, "HASH";
56 $yes = isa "Foo", "Bar";
57
58 or
59
60 require UNIVERSAL ;
61 $yes = UNIVERSAL::isa $a, "ARRAY";
62
63 "isa" returns true in the same cases as above and also if "VAL" is
64 an unblessed reference to a perl variable of type "TYPE", such as
65 "HASH", "ARRAY", or "Regexp".
66
67 "$obj->can( METHOD )"
68 "CLASS->can( METHOD )"
69 "can( VAL, METHOD )"
70 "can" checks if the object or class has a method called "METHOD".
71 If it does then a reference to the sub is returned. If it does not
72 then undef is returned. This includes methods inherited or
73 imported by $obj, "CLASS", or "VAL".
74
75 "can" cannot know whether an object will be able to provide a
76 method through AUTOLOAD, so a return value of undef does not neces‐
77 sarily mean the object will not be able to handle the method call.
78 To get around this some module authors use a forward declaration
79 (see perlsub) for methods they will handle via AUTOLOAD. For such
80 'dummy' subs, "can" will still return a code reference, which, when
81 called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD
82 is provided, calling the coderef will cause an error.
83
84 "can" can be called as a class (static) method, an object method,
85 or a function.
86
87 When used as a function, if "VAL" is a blessed reference or package
88 name which has a method called "METHOD", "can" returns a reference
89 to the subroutine. If "VAL" is not a blessed reference, or if it
90 does not have a method "METHOD", undef is returned.
91
92 "VERSION ( [ REQUIRE ] )"
93 "VERSION" will return the value of the variable $VERSION in the
94 package the object is blessed into. If "REQUIRE" is given then it
95 will do a comparison and die if the package version is not greater
96 than or equal to "REQUIRE".
97
98 "VERSION" can be called as either a class (static) method, an
99 object method or a function.
100
102 None by default.
103
104 You may request the import of all three functions ("isa", "can", and
105 "VERSION"), however it isn't usually necessary to do so. Perl magi‐
106 cally makes these functions act as methods on all objects. The one
107 exception is "isa", which is useful as a function when operating on
108 non-blessed references.
109
110
111
112perl v5.8.8 2001-09-21 UNIVERSAL(3pm)