1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlobj - Perl objects 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateFirst you need to understand what references are in Perl. 8*0Sstevel@tonic-gateSee L<perlref> for that. Second, if you still find the following 9*0Sstevel@tonic-gatereference work too complicated, a tutorial on object-oriented programming 10*0Sstevel@tonic-gatein Perl can be found in L<perltoot> and L<perltooc>. 11*0Sstevel@tonic-gate 12*0Sstevel@tonic-gateIf you're still with us, then 13*0Sstevel@tonic-gatehere are three very simple definitions that you should find reassuring. 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gate=over 4 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate=item 1. 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gateAn object is simply a reference that happens to know which class it 20*0Sstevel@tonic-gatebelongs to. 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate=item 2. 23*0Sstevel@tonic-gate 24*0Sstevel@tonic-gateA class is simply a package that happens to provide methods to deal 25*0Sstevel@tonic-gatewith object references. 26*0Sstevel@tonic-gate 27*0Sstevel@tonic-gate=item 3. 28*0Sstevel@tonic-gate 29*0Sstevel@tonic-gateA method is simply a subroutine that expects an object reference (or 30*0Sstevel@tonic-gatea package name, for class methods) as the first argument. 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate=back 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gateWe'll cover these points now in more depth. 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate=head2 An Object is Simply a Reference 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateUnlike say C++, Perl doesn't provide any special syntax for 39*0Sstevel@tonic-gateconstructors. A constructor is merely a subroutine that returns a 40*0Sstevel@tonic-gatereference to something "blessed" into a class, generally the 41*0Sstevel@tonic-gateclass that the subroutine is defined in. Here is a typical 42*0Sstevel@tonic-gateconstructor: 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate package Critter; 45*0Sstevel@tonic-gate sub new { bless {} } 46*0Sstevel@tonic-gate 47*0Sstevel@tonic-gateThat word C<new> isn't special. You could have written 48*0Sstevel@tonic-gatea construct this way, too: 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gate package Critter; 51*0Sstevel@tonic-gate sub spawn { bless {} } 52*0Sstevel@tonic-gate 53*0Sstevel@tonic-gateThis might even be preferable, because the C++ programmers won't 54*0Sstevel@tonic-gatebe tricked into thinking that C<new> works in Perl as it does in C++. 55*0Sstevel@tonic-gateIt doesn't. We recommend that you name your constructors whatever 56*0Sstevel@tonic-gatemakes sense in the context of the problem you're solving. For example, 57*0Sstevel@tonic-gateconstructors in the Tk extension to Perl are named after the widgets 58*0Sstevel@tonic-gatethey create. 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gateOne thing that's different about Perl constructors compared with those in 61*0Sstevel@tonic-gateC++ is that in Perl, they have to allocate their own memory. (The other 62*0Sstevel@tonic-gatethings is that they don't automatically call overridden base-class 63*0Sstevel@tonic-gateconstructors.) The C<{}> allocates an anonymous hash containing no 64*0Sstevel@tonic-gatekey/value pairs, and returns it The bless() takes that reference and 65*0Sstevel@tonic-gatetells the object it references that it's now a Critter, and returns 66*0Sstevel@tonic-gatethe reference. This is for convenience, because the referenced object 67*0Sstevel@tonic-gateitself knows that it has been blessed, and the reference to it could 68*0Sstevel@tonic-gatehave been returned directly, like this: 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate sub new { 71*0Sstevel@tonic-gate my $self = {}; 72*0Sstevel@tonic-gate bless $self; 73*0Sstevel@tonic-gate return $self; 74*0Sstevel@tonic-gate } 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gateYou often see such a thing in more complicated constructors 77*0Sstevel@tonic-gatethat wish to call methods in the class as part of the construction: 78*0Sstevel@tonic-gate 79*0Sstevel@tonic-gate sub new { 80*0Sstevel@tonic-gate my $self = {}; 81*0Sstevel@tonic-gate bless $self; 82*0Sstevel@tonic-gate $self->initialize(); 83*0Sstevel@tonic-gate return $self; 84*0Sstevel@tonic-gate } 85*0Sstevel@tonic-gate 86*0Sstevel@tonic-gateIf you care about inheritance (and you should; see 87*0Sstevel@tonic-gateL<perlmodlib/"Modules: Creation, Use, and Abuse">), 88*0Sstevel@tonic-gatethen you want to use the two-arg form of bless 89*0Sstevel@tonic-gateso that your constructors may be inherited: 90*0Sstevel@tonic-gate 91*0Sstevel@tonic-gate sub new { 92*0Sstevel@tonic-gate my $class = shift; 93*0Sstevel@tonic-gate my $self = {}; 94*0Sstevel@tonic-gate bless $self, $class; 95*0Sstevel@tonic-gate $self->initialize(); 96*0Sstevel@tonic-gate return $self; 97*0Sstevel@tonic-gate } 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gateOr if you expect people to call not just C<< CLASS->new() >> but also 100*0Sstevel@tonic-gateC<< $obj->new() >>, then use something like the following. (Note that using 101*0Sstevel@tonic-gatethis to call new() on an instance does not automatically perform any 102*0Sstevel@tonic-gatecopying. If you want a shallow or deep copy of an object, you'll have to 103*0Sstevel@tonic-gatespecifically allow for that.) The initialize() method used will be of 104*0Sstevel@tonic-gatewhatever $class we blessed the object into: 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate sub new { 107*0Sstevel@tonic-gate my $this = shift; 108*0Sstevel@tonic-gate my $class = ref($this) || $this; 109*0Sstevel@tonic-gate my $self = {}; 110*0Sstevel@tonic-gate bless $self, $class; 111*0Sstevel@tonic-gate $self->initialize(); 112*0Sstevel@tonic-gate return $self; 113*0Sstevel@tonic-gate } 114*0Sstevel@tonic-gate 115*0Sstevel@tonic-gateWithin the class package, the methods will typically deal with the 116*0Sstevel@tonic-gatereference as an ordinary reference. Outside the class package, 117*0Sstevel@tonic-gatethe reference is generally treated as an opaque value that may 118*0Sstevel@tonic-gatebe accessed only through the class's methods. 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateAlthough a constructor can in theory re-bless a referenced object 121*0Sstevel@tonic-gatecurrently belonging to another class, this is almost certainly going 122*0Sstevel@tonic-gateto get you into trouble. The new class is responsible for all 123*0Sstevel@tonic-gatecleanup later. The previous blessing is forgotten, as an object 124*0Sstevel@tonic-gatemay belong to only one class at a time. (Although of course it's 125*0Sstevel@tonic-gatefree to inherit methods from many classes.) If you find yourself 126*0Sstevel@tonic-gatehaving to do this, the parent class is probably misbehaving, though. 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gateA clarification: Perl objects are blessed. References are not. Objects 129*0Sstevel@tonic-gateknow which package they belong to. References do not. The bless() 130*0Sstevel@tonic-gatefunction uses the reference to find the object. Consider 131*0Sstevel@tonic-gatethe following example: 132*0Sstevel@tonic-gate 133*0Sstevel@tonic-gate $a = {}; 134*0Sstevel@tonic-gate $b = $a; 135*0Sstevel@tonic-gate bless $a, BLAH; 136*0Sstevel@tonic-gate print "\$b is a ", ref($b), "\n"; 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gateThis reports $b as being a BLAH, so obviously bless() 139*0Sstevel@tonic-gateoperated on the object and not on the reference. 140*0Sstevel@tonic-gate 141*0Sstevel@tonic-gate=head2 A Class is Simply a Package 142*0Sstevel@tonic-gate 143*0Sstevel@tonic-gateUnlike say C++, Perl doesn't provide any special syntax for class 144*0Sstevel@tonic-gatedefinitions. You use a package as a class by putting method 145*0Sstevel@tonic-gatedefinitions into the class. 146*0Sstevel@tonic-gate 147*0Sstevel@tonic-gateThere is a special array within each package called @ISA, which says 148*0Sstevel@tonic-gatewhere else to look for a method if you can't find it in the current 149*0Sstevel@tonic-gatepackage. This is how Perl implements inheritance. Each element of the 150*0Sstevel@tonic-gate@ISA array is just the name of another package that happens to be a 151*0Sstevel@tonic-gateclass package. The classes are searched (depth first) for missing 152*0Sstevel@tonic-gatemethods in the order that they occur in @ISA. The classes accessible 153*0Sstevel@tonic-gatethrough @ISA are known as base classes of the current class. 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gateAll classes implicitly inherit from class C<UNIVERSAL> as their 156*0Sstevel@tonic-gatelast base class. Several commonly used methods are automatically 157*0Sstevel@tonic-gatesupplied in the UNIVERSAL class; see L<"Default UNIVERSAL methods"> for 158*0Sstevel@tonic-gatemore details. 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gateIf a missing method is found in a base class, it is cached 161*0Sstevel@tonic-gatein the current class for efficiency. Changing @ISA or defining new 162*0Sstevel@tonic-gatesubroutines invalidates the cache and causes Perl to do the lookup again. 163*0Sstevel@tonic-gate 164*0Sstevel@tonic-gateIf neither the current class, its named base classes, nor the UNIVERSAL 165*0Sstevel@tonic-gateclass contains the requested method, these three places are searched 166*0Sstevel@tonic-gateall over again, this time looking for a method named AUTOLOAD(). If an 167*0Sstevel@tonic-gateAUTOLOAD is found, this method is called on behalf of the missing method, 168*0Sstevel@tonic-gatesetting the package global $AUTOLOAD to be the fully qualified name of 169*0Sstevel@tonic-gatethe method that was intended to be called. 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gateIf none of that works, Perl finally gives up and complains. 172*0Sstevel@tonic-gate 173*0Sstevel@tonic-gateIf you want to stop the AUTOLOAD inheritance say simply 174*0Sstevel@tonic-gate 175*0Sstevel@tonic-gate sub AUTOLOAD; 176*0Sstevel@tonic-gate 177*0Sstevel@tonic-gateand the call will die using the name of the sub being called. 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gatePerl classes do method inheritance only. Data inheritance is left up 180*0Sstevel@tonic-gateto the class itself. By and large, this is not a problem in Perl, 181*0Sstevel@tonic-gatebecause most classes model the attributes of their object using an 182*0Sstevel@tonic-gateanonymous hash, which serves as its own little namespace to be carved up 183*0Sstevel@tonic-gateby the various classes that might want to do something with the object. 184*0Sstevel@tonic-gateThe only problem with this is that you can't sure that you aren't using 185*0Sstevel@tonic-gatea piece of the hash that isn't already used. A reasonable workaround 186*0Sstevel@tonic-gateis to prepend your fieldname in the hash with the package name. 187*0Sstevel@tonic-gate 188*0Sstevel@tonic-gate sub bump { 189*0Sstevel@tonic-gate my $self = shift; 190*0Sstevel@tonic-gate $self->{ __PACKAGE__ . ".count"}++; 191*0Sstevel@tonic-gate } 192*0Sstevel@tonic-gate 193*0Sstevel@tonic-gate=head2 A Method is Simply a Subroutine 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gateUnlike say C++, Perl doesn't provide any special syntax for method 196*0Sstevel@tonic-gatedefinition. (It does provide a little syntax for method invocation 197*0Sstevel@tonic-gatethough. More on that later.) A method expects its first argument 198*0Sstevel@tonic-gateto be the object (reference) or package (string) it is being invoked 199*0Sstevel@tonic-gateon. There are two ways of calling methods, which we'll call class 200*0Sstevel@tonic-gatemethods and instance methods. 201*0Sstevel@tonic-gate 202*0Sstevel@tonic-gateA class method expects a class name as the first argument. It 203*0Sstevel@tonic-gateprovides functionality for the class as a whole, not for any 204*0Sstevel@tonic-gateindividual object belonging to the class. Constructors are often 205*0Sstevel@tonic-gateclass methods, but see L<perltoot> and L<perltooc> for alternatives. 206*0Sstevel@tonic-gateMany class methods simply ignore their first argument, because they 207*0Sstevel@tonic-gatealready know what package they're in and don't care what package 208*0Sstevel@tonic-gatethey were invoked via. (These aren't necessarily the same, because 209*0Sstevel@tonic-gateclass methods follow the inheritance tree just like ordinary instance 210*0Sstevel@tonic-gatemethods.) Another typical use for class methods is to look up an 211*0Sstevel@tonic-gateobject by name: 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate sub find { 214*0Sstevel@tonic-gate my ($class, $name) = @_; 215*0Sstevel@tonic-gate $objtable{$name}; 216*0Sstevel@tonic-gate } 217*0Sstevel@tonic-gate 218*0Sstevel@tonic-gateAn instance method expects an object reference as its first argument. 219*0Sstevel@tonic-gateTypically it shifts the first argument into a "self" or "this" variable, 220*0Sstevel@tonic-gateand then uses that as an ordinary reference. 221*0Sstevel@tonic-gate 222*0Sstevel@tonic-gate sub display { 223*0Sstevel@tonic-gate my $self = shift; 224*0Sstevel@tonic-gate my @keys = @_ ? @_ : sort keys %$self; 225*0Sstevel@tonic-gate foreach $key (@keys) { 226*0Sstevel@tonic-gate print "\t$key => $self->{$key}\n"; 227*0Sstevel@tonic-gate } 228*0Sstevel@tonic-gate } 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gate=head2 Method Invocation 231*0Sstevel@tonic-gate 232*0Sstevel@tonic-gateFor various historical and other reasons, Perl offers two equivalent 233*0Sstevel@tonic-gateways to write a method call. The simpler and more common way is to use 234*0Sstevel@tonic-gatethe arrow notation: 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate my $fred = Critter->find("Fred"); 237*0Sstevel@tonic-gate $fred->display("Height", "Weight"); 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gateYou should already be familiar with the use of the C<< -> >> operator with 240*0Sstevel@tonic-gatereferences. In fact, since C<$fred> above is a reference to an object, 241*0Sstevel@tonic-gateyou could think of the method call as just another form of 242*0Sstevel@tonic-gatedereferencing. 243*0Sstevel@tonic-gate 244*0Sstevel@tonic-gateWhatever is on the left side of the arrow, whether a reference or a 245*0Sstevel@tonic-gateclass name, is passed to the method subroutine as its first argument. 246*0Sstevel@tonic-gateSo the above code is mostly equivalent to: 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate my $fred = Critter::find("Critter", "Fred"); 249*0Sstevel@tonic-gate Critter::display($fred, "Height", "Weight"); 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gateHow does Perl know which package the subroutine is in? By looking at 252*0Sstevel@tonic-gatethe left side of the arrow, which must be either a package name or a 253*0Sstevel@tonic-gatereference to an object, i.e. something that has been blessed to a 254*0Sstevel@tonic-gatepackage. Either way, that's the package where Perl starts looking. If 255*0Sstevel@tonic-gatethat package has no subroutine with that name, Perl starts looking for 256*0Sstevel@tonic-gateit in any base classes of that package, and so on. 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gateIf you need to, you I<can> force Perl to start looking in some other package: 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gate my $barney = MyCritter->Critter::find("Barney"); 261*0Sstevel@tonic-gate $barney->Critter::display("Height", "Weight"); 262*0Sstevel@tonic-gate 263*0Sstevel@tonic-gateHere C<MyCritter> is presumably a subclass of C<Critter> that defines 264*0Sstevel@tonic-gateits own versions of find() and display(). We haven't specified what 265*0Sstevel@tonic-gatethose methods do, but that doesn't matter above since we've forced Perl 266*0Sstevel@tonic-gateto start looking for the subroutines in C<Critter>. 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gateAs a special case of the above, you may use the C<SUPER> pseudo-class to 269*0Sstevel@tonic-gatetell Perl to start looking for the method in the packages named in the 270*0Sstevel@tonic-gatecurrent class's C<@ISA> list. 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate package MyCritter; 273*0Sstevel@tonic-gate use base 'Critter'; # sets @MyCritter::ISA = ('Critter'); 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate sub display { 276*0Sstevel@tonic-gate my ($self, @args) = @_; 277*0Sstevel@tonic-gate $self->SUPER::display("Name", @args); 278*0Sstevel@tonic-gate } 279*0Sstevel@tonic-gate 280*0Sstevel@tonic-gateIt is important to note that C<SUPER> refers to the superclass(es) of the 281*0Sstevel@tonic-gateI<current package> and not to the superclass(es) of the object. Also, the 282*0Sstevel@tonic-gateC<SUPER> pseudo-class can only currently be used as a modifier to a method 283*0Sstevel@tonic-gatename, but not in any of the other ways that class names are normally used, 284*0Sstevel@tonic-gateeg: 285*0Sstevel@tonic-gate 286*0Sstevel@tonic-gate something->SUPER::method(...); # OK 287*0Sstevel@tonic-gate SUPER::method(...); # WRONG 288*0Sstevel@tonic-gate SUPER->method(...); # WRONG 289*0Sstevel@tonic-gate 290*0Sstevel@tonic-gateInstead of a class name or an object reference, you can also use any 291*0Sstevel@tonic-gateexpression that returns either of those on the left side of the arrow. 292*0Sstevel@tonic-gateSo the following statement is valid: 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gate Critter->find("Fred")->display("Height", "Weight"); 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gateand so is the following: 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gate my $fred = (reverse "rettirC")->find(reverse "derF"); 299*0Sstevel@tonic-gate 300*0Sstevel@tonic-gate=head2 Indirect Object Syntax 301*0Sstevel@tonic-gate 302*0Sstevel@tonic-gateThe other way to invoke a method is by using the so-called "indirect 303*0Sstevel@tonic-gateobject" notation. This syntax was available in Perl 4 long before 304*0Sstevel@tonic-gateobjects were introduced, and is still used with filehandles like this: 305*0Sstevel@tonic-gate 306*0Sstevel@tonic-gate print STDERR "help!!!\n"; 307*0Sstevel@tonic-gate 308*0Sstevel@tonic-gateThe same syntax can be used to call either object or class methods. 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gate my $fred = find Critter "Fred"; 311*0Sstevel@tonic-gate display $fred "Height", "Weight"; 312*0Sstevel@tonic-gate 313*0Sstevel@tonic-gateNotice that there is no comma between the object or class name and the 314*0Sstevel@tonic-gateparameters. This is how Perl can tell you want an indirect method call 315*0Sstevel@tonic-gateinstead of an ordinary subroutine call. 316*0Sstevel@tonic-gate 317*0Sstevel@tonic-gateBut what if there are no arguments? In that case, Perl must guess what 318*0Sstevel@tonic-gateyou want. Even worse, it must make that guess I<at compile time>. 319*0Sstevel@tonic-gateUsually Perl gets it right, but when it doesn't you get a function 320*0Sstevel@tonic-gatecall compiled as a method, or vice versa. This can introduce subtle bugs 321*0Sstevel@tonic-gatethat are hard to detect. 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gateFor example, a call to a method C<new> in indirect notation -- as C++ 324*0Sstevel@tonic-gateprogrammers are wont to make -- can be miscompiled into a subroutine 325*0Sstevel@tonic-gatecall if there's already a C<new> function in scope. You'd end up 326*0Sstevel@tonic-gatecalling the current package's C<new> as a subroutine, rather than the 327*0Sstevel@tonic-gatedesired class's method. The compiler tries to cheat by remembering 328*0Sstevel@tonic-gatebareword C<require>s, but the grief when it messes up just isn't worth the 329*0Sstevel@tonic-gateyears of debugging it will take you to track down such subtle bugs. 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gateThere is another problem with this syntax: the indirect object is 332*0Sstevel@tonic-gatelimited to a name, a scalar variable, or a block, because it would have 333*0Sstevel@tonic-gateto do too much lookahead otherwise, just like any other postfix 334*0Sstevel@tonic-gatedereference in the language. (These are the same quirky rules as are 335*0Sstevel@tonic-gateused for the filehandle slot in functions like C<print> and C<printf>.) 336*0Sstevel@tonic-gateThis can lead to horribly confusing precedence problems, as in these 337*0Sstevel@tonic-gatenext two lines: 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate move $obj->{FIELD}; # probably wrong! 340*0Sstevel@tonic-gate move $ary[$i]; # probably wrong! 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gateThose actually parse as the very surprising: 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gate $obj->move->{FIELD}; # Well, lookee here 345*0Sstevel@tonic-gate $ary->move([$i]); # Didn't expect this one, eh? 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gateRather than what you might have expected: 348*0Sstevel@tonic-gate 349*0Sstevel@tonic-gate $obj->{FIELD}->move(); # You should be so lucky. 350*0Sstevel@tonic-gate $ary[$i]->move; # Yeah, sure. 351*0Sstevel@tonic-gate 352*0Sstevel@tonic-gateTo get the correct behavior with indirect object syntax, you would have 353*0Sstevel@tonic-gateto use a block around the indirect object: 354*0Sstevel@tonic-gate 355*0Sstevel@tonic-gate move {$obj->{FIELD}}; 356*0Sstevel@tonic-gate move {$ary[$i]}; 357*0Sstevel@tonic-gate 358*0Sstevel@tonic-gateEven then, you still have the same potential problem if there happens to 359*0Sstevel@tonic-gatebe a function named C<move> in the current package. B<The C<< -> >> 360*0Sstevel@tonic-gatenotation suffers from neither of these disturbing ambiguities, so we 361*0Sstevel@tonic-gaterecommend you use it exclusively.> However, you may still end up having 362*0Sstevel@tonic-gateto read code using the indirect object notation, so it's important to be 363*0Sstevel@tonic-gatefamiliar with it. 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gate=head2 Default UNIVERSAL methods 366*0Sstevel@tonic-gate 367*0Sstevel@tonic-gateThe C<UNIVERSAL> package automatically contains the following methods that 368*0Sstevel@tonic-gateare inherited by all other classes: 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gate=over 4 371*0Sstevel@tonic-gate 372*0Sstevel@tonic-gate=item isa(CLASS) 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gateC<isa> returns I<true> if its object is blessed into a subclass of C<CLASS> 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gateYou can also call C<UNIVERSAL::isa> as a subroutine with two arguments. 377*0Sstevel@tonic-gateThe first does not need to be an object or even a reference. This 378*0Sstevel@tonic-gateallows you to check what a reference points to, or whether 379*0Sstevel@tonic-gatesomething is a reference of a given type. Example 380*0Sstevel@tonic-gate 381*0Sstevel@tonic-gate if(UNIVERSAL::isa($ref, 'ARRAY')) { 382*0Sstevel@tonic-gate #... 383*0Sstevel@tonic-gate } 384*0Sstevel@tonic-gate 385*0Sstevel@tonic-gateTo determine if a reference is a blessed object, you can write 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate print "It's an object\n" if UNIVERSAL::isa($val, 'UNIVERSAL'); 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gate=item can(METHOD) 390*0Sstevel@tonic-gate 391*0Sstevel@tonic-gateC<can> checks to see if its object has a method called C<METHOD>, 392*0Sstevel@tonic-gateif it does then a reference to the sub is returned, if it does not then 393*0Sstevel@tonic-gateI<undef> is returned. 394*0Sstevel@tonic-gate 395*0Sstevel@tonic-gateC<UNIVERSAL::can> can also be called as a subroutine with two arguments. 396*0Sstevel@tonic-gateIt'll always return I<undef> if its first argument isn't an object or a 397*0Sstevel@tonic-gateclass name. So here's another way to check if a reference is a 398*0Sstevel@tonic-gateblessed object 399*0Sstevel@tonic-gate 400*0Sstevel@tonic-gate print "It's still an object\n" if UNIVERSAL::can($val, 'can'); 401*0Sstevel@tonic-gate 402*0Sstevel@tonic-gateYou can also use the C<blessed> function of Scalar::Util: 403*0Sstevel@tonic-gate 404*0Sstevel@tonic-gate use Scalar::Util 'blessed'; 405*0Sstevel@tonic-gate 406*0Sstevel@tonic-gate my $blessing = blessed $suspected_object; 407*0Sstevel@tonic-gate 408*0Sstevel@tonic-gateC<blessed> returns the name of the package the argument has been 409*0Sstevel@tonic-gateblessed into, or C<undef>. 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate=item VERSION( [NEED] ) 412*0Sstevel@tonic-gate 413*0Sstevel@tonic-gateC<VERSION> returns the version number of the class (package). If the 414*0Sstevel@tonic-gateNEED argument is given then it will check that the current version (as 415*0Sstevel@tonic-gatedefined by the $VERSION variable in the given package) not less than 416*0Sstevel@tonic-gateNEED; it will die if this is not the case. This method is normally 417*0Sstevel@tonic-gatecalled as a class method. This method is called automatically by the 418*0Sstevel@tonic-gateC<VERSION> form of C<use>. 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gate use A 1.2 qw(some imported subs); 421*0Sstevel@tonic-gate # implies: 422*0Sstevel@tonic-gate A->VERSION(1.2); 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate=back 425*0Sstevel@tonic-gate 426*0Sstevel@tonic-gateB<NOTE:> C<can> directly uses Perl's internal code for method lookup, and 427*0Sstevel@tonic-gateC<isa> uses a very similar method and cache-ing strategy. This may cause 428*0Sstevel@tonic-gatestrange effects if the Perl code dynamically changes @ISA in any package. 429*0Sstevel@tonic-gate 430*0Sstevel@tonic-gateYou may add other methods to the UNIVERSAL class via Perl or XS code. 431*0Sstevel@tonic-gateYou do not need to C<use UNIVERSAL> to make these methods 432*0Sstevel@tonic-gateavailable to your program (and you should not do so). 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gate=head2 Destructors 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gateWhen the last reference to an object goes away, the object is 437*0Sstevel@tonic-gateautomatically destroyed. (This may even be after you exit, if you've 438*0Sstevel@tonic-gatestored references in global variables.) If you want to capture control 439*0Sstevel@tonic-gatejust before the object is freed, you may define a DESTROY method in 440*0Sstevel@tonic-gateyour class. It will automatically be called at the appropriate moment, 441*0Sstevel@tonic-gateand you can do any extra cleanup you need to do. Perl passes a reference 442*0Sstevel@tonic-gateto the object under destruction as the first (and only) argument. Beware 443*0Sstevel@tonic-gatethat the reference is a read-only value, and cannot be modified by 444*0Sstevel@tonic-gatemanipulating C<$_[0]> within the destructor. The object itself (i.e. 445*0Sstevel@tonic-gatethe thingy the reference points to, namely C<${$_[0]}>, C<@{$_[0]}>, 446*0Sstevel@tonic-gateC<%{$_[0]}> etc.) is not similarly constrained. 447*0Sstevel@tonic-gate 448*0Sstevel@tonic-gateIf you arrange to re-bless the reference before the destructor returns, 449*0Sstevel@tonic-gateperl will again call the DESTROY method for the re-blessed object after 450*0Sstevel@tonic-gatethe current one returns. This can be used for clean delegation of 451*0Sstevel@tonic-gateobject destruction, or for ensuring that destructors in the base classes 452*0Sstevel@tonic-gateof your choosing get called. Explicitly calling DESTROY is also possible, 453*0Sstevel@tonic-gatebut is usually never needed. 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gateDo not confuse the previous discussion with how objects I<CONTAINED> in the current 456*0Sstevel@tonic-gateone are destroyed. Such objects will be freed and destroyed automatically 457*0Sstevel@tonic-gatewhen the current object is freed, provided no other references to them exist 458*0Sstevel@tonic-gateelsewhere. 459*0Sstevel@tonic-gate 460*0Sstevel@tonic-gate=head2 Summary 461*0Sstevel@tonic-gate 462*0Sstevel@tonic-gateThat's about all there is to it. Now you need just to go off and buy a 463*0Sstevel@tonic-gatebook about object-oriented design methodology, and bang your forehead 464*0Sstevel@tonic-gatewith it for the next six months or so. 465*0Sstevel@tonic-gate 466*0Sstevel@tonic-gate=head2 Two-Phased Garbage Collection 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gateFor most purposes, Perl uses a fast and simple, reference-based 469*0Sstevel@tonic-gategarbage collection system. That means there's an extra 470*0Sstevel@tonic-gatedereference going on at some level, so if you haven't built 471*0Sstevel@tonic-gateyour Perl executable using your C compiler's C<-O> flag, performance 472*0Sstevel@tonic-gatewill suffer. If you I<have> built Perl with C<cc -O>, then this 473*0Sstevel@tonic-gateprobably won't matter. 474*0Sstevel@tonic-gate 475*0Sstevel@tonic-gateA more serious concern is that unreachable memory with a non-zero 476*0Sstevel@tonic-gatereference count will not normally get freed. Therefore, this is a bad 477*0Sstevel@tonic-gateidea: 478*0Sstevel@tonic-gate 479*0Sstevel@tonic-gate { 480*0Sstevel@tonic-gate my $a; 481*0Sstevel@tonic-gate $a = \$a; 482*0Sstevel@tonic-gate } 483*0Sstevel@tonic-gate 484*0Sstevel@tonic-gateEven thought $a I<should> go away, it can't. When building recursive data 485*0Sstevel@tonic-gatestructures, you'll have to break the self-reference yourself explicitly 486*0Sstevel@tonic-gateif you don't care to leak. For example, here's a self-referential 487*0Sstevel@tonic-gatenode such as one might use in a sophisticated tree structure: 488*0Sstevel@tonic-gate 489*0Sstevel@tonic-gate sub new_node { 490*0Sstevel@tonic-gate my $class = shift; 491*0Sstevel@tonic-gate my $node = {}; 492*0Sstevel@tonic-gate $node->{LEFT} = $node->{RIGHT} = $node; 493*0Sstevel@tonic-gate $node->{DATA} = [ @_ ]; 494*0Sstevel@tonic-gate return bless $node => $class; 495*0Sstevel@tonic-gate } 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gateIf you create nodes like that, they (currently) won't go away unless you 498*0Sstevel@tonic-gatebreak their self reference yourself. (In other words, this is not to be 499*0Sstevel@tonic-gateconstrued as a feature, and you shouldn't depend on it.) 500*0Sstevel@tonic-gate 501*0Sstevel@tonic-gateAlmost. 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gateWhen an interpreter thread finally shuts down (usually when your program 504*0Sstevel@tonic-gateexits), then a rather costly but complete mark-and-sweep style of garbage 505*0Sstevel@tonic-gatecollection is performed, and everything allocated by that thread gets 506*0Sstevel@tonic-gatedestroyed. This is essential to support Perl as an embedded or a 507*0Sstevel@tonic-gatemultithreadable language. For example, this program demonstrates Perl's 508*0Sstevel@tonic-gatetwo-phased garbage collection: 509*0Sstevel@tonic-gate 510*0Sstevel@tonic-gate #!/usr/bin/perl 511*0Sstevel@tonic-gate package Subtle; 512*0Sstevel@tonic-gate 513*0Sstevel@tonic-gate sub new { 514*0Sstevel@tonic-gate my $test; 515*0Sstevel@tonic-gate $test = \$test; 516*0Sstevel@tonic-gate warn "CREATING " . \$test; 517*0Sstevel@tonic-gate return bless \$test; 518*0Sstevel@tonic-gate } 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate sub DESTROY { 521*0Sstevel@tonic-gate my $self = shift; 522*0Sstevel@tonic-gate warn "DESTROYING $self"; 523*0Sstevel@tonic-gate } 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate package main; 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gate warn "starting program"; 528*0Sstevel@tonic-gate { 529*0Sstevel@tonic-gate my $a = Subtle->new; 530*0Sstevel@tonic-gate my $b = Subtle->new; 531*0Sstevel@tonic-gate $$a = 0; # break selfref 532*0Sstevel@tonic-gate warn "leaving block"; 533*0Sstevel@tonic-gate } 534*0Sstevel@tonic-gate 535*0Sstevel@tonic-gate warn "just exited block"; 536*0Sstevel@tonic-gate warn "time to die..."; 537*0Sstevel@tonic-gate exit; 538*0Sstevel@tonic-gate 539*0Sstevel@tonic-gateWhen run as F</foo/test>, the following output is produced: 540*0Sstevel@tonic-gate 541*0Sstevel@tonic-gate starting program at /foo/test line 18. 542*0Sstevel@tonic-gate CREATING SCALAR(0x8e5b8) at /foo/test line 7. 543*0Sstevel@tonic-gate CREATING SCALAR(0x8e57c) at /foo/test line 7. 544*0Sstevel@tonic-gate leaving block at /foo/test line 23. 545*0Sstevel@tonic-gate DESTROYING Subtle=SCALAR(0x8e5b8) at /foo/test line 13. 546*0Sstevel@tonic-gate just exited block at /foo/test line 26. 547*0Sstevel@tonic-gate time to die... at /foo/test line 27. 548*0Sstevel@tonic-gate DESTROYING Subtle=SCALAR(0x8e57c) during global destruction. 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gateNotice that "global destruction" bit there? That's the thread 551*0Sstevel@tonic-gategarbage collector reaching the unreachable. 552*0Sstevel@tonic-gate 553*0Sstevel@tonic-gateObjects are always destructed, even when regular refs aren't. Objects 554*0Sstevel@tonic-gateare destructed in a separate pass before ordinary refs just to 555*0Sstevel@tonic-gateprevent object destructors from using refs that have been themselves 556*0Sstevel@tonic-gatedestructed. Plain refs are only garbage-collected if the destruct level 557*0Sstevel@tonic-gateis greater than 0. You can test the higher levels of global destruction 558*0Sstevel@tonic-gateby setting the PERL_DESTRUCT_LEVEL environment variable, presuming 559*0Sstevel@tonic-gateC<-DDEBUGGING> was enabled during perl build time. 560*0Sstevel@tonic-gateSee L<perlhack/PERL_DESTRUCT_LEVEL> for more information. 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gateA more complete garbage collection strategy will be implemented 563*0Sstevel@tonic-gateat a future date. 564*0Sstevel@tonic-gate 565*0Sstevel@tonic-gateIn the meantime, the best solution is to create a non-recursive container 566*0Sstevel@tonic-gateclass that holds a pointer to the self-referential data structure. 567*0Sstevel@tonic-gateDefine a DESTROY method for the containing object's class that manually 568*0Sstevel@tonic-gatebreaks the circularities in the self-referential structure. 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gate=head1 SEE ALSO 571*0Sstevel@tonic-gate 572*0Sstevel@tonic-gateA kinder, gentler tutorial on object-oriented programming in Perl can 573*0Sstevel@tonic-gatebe found in L<perltoot>, L<perlboot> and L<perltooc>. You should 574*0Sstevel@tonic-gatealso check out L<perlbot> for other object tricks, traps, and tips, as 575*0Sstevel@tonic-gatewell as L<perlmodlib> for some style guides on constructing both 576*0Sstevel@tonic-gatemodules and classes. 577