1package UNIVERSAL; 2 3our $VERSION = '1.05'; 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. 11require 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. 16sub import { 17 return unless $_[0] eq __PACKAGE__; 18 goto &Exporter::import; 19} 20 211; 22__END__ 23 24=head1 NAME 25 26UNIVERSAL - 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 $does_log = $obj->DOES("Logger"); 34 $does_log = Class->DOES("Logger"); 35 36 $sub = $obj->can("print"); 37 $sub = Class->can("print"); 38 39 $sub = eval { $ref->can("fandango") }; 40 $ver = $obj->VERSION; 41 42 # but never do this! 43 $is_io = UNIVERSAL::isa($fd, "IO::Handle"); 44 $sub = UNIVERSAL::can($obj, "print"); 45 46=head1 DESCRIPTION 47 48C<UNIVERSAL> is the base class from which all blessed references inherit. 49See L<perlobj>. 50 51C<UNIVERSAL> provides the following methods: 52 53=over 4 54 55=item C<< $obj->isa( TYPE ) >> 56 57=item C<< CLASS->isa( TYPE ) >> 58 59=item C<< eval { VAL->isa( TYPE ) } >> 60 61Where 62 63=over 4 64 65=item C<TYPE> 66 67is a package name 68 69=item C<$obj> 70 71is a blessed reference or a package name 72 73=item C<CLASS> 74 75is a package name 76 77=item C<VAL> 78 79is any of the above or an unblessed reference 80 81=back 82 83When used as an instance or class method (C<< $obj->isa( TYPE ) >>), 84C<isa> returns I<true> if $obj is blessed into package C<TYPE> or 85inherits from package C<TYPE>. 86 87When used as a class method (C<< CLASS->isa( TYPE ) >>, sometimes 88referred to as a static method), C<isa> returns I<true> if C<CLASS> 89inherits from (or is itself) the name of the package C<TYPE> or 90inherits from package C<TYPE>. 91 92If you're not sure what you have (the C<VAL> case), wrap the method call in an 93C<eval> block to catch the exception if C<VAL> is undefined. 94 95If you want to be sure that you're calling C<isa> as a method, not a class, 96check the invocant with C<blessed> from L<Scalar::Util> first: 97 98 use Scalar::Util 'blessed'; 99 100 if ( blessed( $obj ) && $obj->isa("Some::Class") { 101 ... 102 } 103 104=item C<< $obj->DOES( ROLE ) >> 105 106=item C<< CLASS->DOES( ROLE ) >> 107 108C<DOES> checks if the object or class performs the role C<ROLE>. A role is a 109named group of specific behavior (often methods of particular names and 110signatures), similar to a class, but not necessarily a complete class by 111itself. For example, logging or serialization may be roles. 112 113C<DOES> and C<isa> are similar, in that if either is true, you know that the 114object or class on which you call the method can perform specific behavior. 115However, C<DOES> is different from C<isa> in that it does not care I<how> the 116invocant performs the operations, merely that it does. (C<isa> of course 117mandates an inheritance relationship. Other relationships include aggregation, 118delegation, and mocking.) 119 120By default, classes in Perl only perform the C<UNIVERSAL> role, as well as the 121role of all classes in their inheritance. In other words, by default C<DOES> 122responds identically to C<isa>. 123 124There is a relationship between roles and classes, as each class implies the 125existence of a role of the same name. There is also a relationship between 126inheritance and roles, in that a subclass that inherits from an ancestor class 127implicitly performs any roles its parent performs. Thus you can use C<DOES> in 128place of C<isa> safely, as it will return true in all places where C<isa> will 129return true (provided that any overridden C<DOES> I<and> C<isa> methods behave 130appropriately). 131 132=item C<< $obj->can( METHOD ) >> 133 134=item C<< CLASS->can( METHOD ) >> 135 136=item C<< eval { VAL->can( METHOD ) } >> 137 138C<can> checks if the object or class has a method called C<METHOD>. If it does, 139then it returns a reference to the sub. If it does not, then it returns 140I<undef>. This includes methods inherited or imported by C<$obj>, C<CLASS>, or 141C<VAL>. 142 143C<can> cannot know whether an object will be able to provide a method through 144AUTOLOAD (unless the object's class has overriden C<can> appropriately), so a 145return value of I<undef> does not necessarily mean the object will not be able 146to handle the method call. To get around this some module authors use a forward 147declaration (see L<perlsub>) for methods they will handle via AUTOLOAD. For 148such 'dummy' subs, C<can> will still return a code reference, which, when 149called, will fall through to the AUTOLOAD. If no suitable AUTOLOAD is provided, 150calling the coderef will cause an error. 151 152You may call C<can> as a class (static) method or an object method. 153 154Again, the same rule about having a valid invocant applies -- use an C<eval> 155block or C<blessed> if you need to be extra paranoid. 156 157=item C<VERSION ( [ REQUIRE ] )> 158 159C<VERSION> will return the value of the variable C<$VERSION> in the 160package the object is blessed into. If C<REQUIRE> is given then 161it will do a comparison and die if the package version is not 162greater than or equal to C<REQUIRE>. 163 164C<VERSION> can be called as either a class (static) method or an object 165method. 166 167=back 168 169=head1 WARNINGS 170 171B<NOTE:> C<can> directly uses Perl's internal code for method lookup, and 172C<isa> uses a very similar method and cache-ing strategy. This may cause 173strange effects if the Perl code dynamically changes @ISA in any package. 174 175You may add other methods to the UNIVERSAL class via Perl or XS code. 176You do not need to C<use UNIVERSAL> to make these methods 177available to your program (and you should not do so). 178 179=head1 EXPORTS 180 181None by default. 182 183You may request the import of three functions (C<isa>, C<can>, and C<VERSION>), 184however it is usually harmful to do so. Please don't do this in new code. 185 186For example, previous versions of this documentation suggested using C<isa> as 187a function to determine the type of a reference: 188 189 use UNIVERSAL 'isa'; 190 191 $yes = isa $h, "HASH"; 192 $yes = isa "Foo", "Bar"; 193 194The problem is that this code will I<never> call an overridden C<isa> method in 195any class. Instead, use C<reftype> from L<Scalar::Util> for the first case: 196 197 use Scalar::Util 'reftype'; 198 199 $yes = reftype( $h ) eq "HASH"; 200 201and the method form of C<isa> for the second: 202 203 $yes = Foo->isa("Bar"); 204 205=cut 206