1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlboot - Beginner's Object-Oriented Tutorial 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateIf you're not familiar with objects from other languages, some of the 8*0Sstevel@tonic-gateother Perl object documentation may be a little daunting, such as 9*0Sstevel@tonic-gateL<perlobj>, a basic reference in using objects, and L<perltoot>, which 10*0Sstevel@tonic-gateintroduces readers to the peculiarities of Perl's object system in a 11*0Sstevel@tonic-gatetutorial way. 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gateSo, let's take a different approach, presuming no prior object 14*0Sstevel@tonic-gateexperience. It helps if you know about subroutines (L<perlsub>), 15*0Sstevel@tonic-gatereferences (L<perlref> et. seq.), and packages (L<perlmod>), so become 16*0Sstevel@tonic-gatefamiliar with those first if you haven't already. 17*0Sstevel@tonic-gate 18*0Sstevel@tonic-gate=head2 If we could talk to the animals... 19*0Sstevel@tonic-gate 20*0Sstevel@tonic-gateLet's let the animals talk for a moment: 21*0Sstevel@tonic-gate 22*0Sstevel@tonic-gate sub Cow::speak { 23*0Sstevel@tonic-gate print "a Cow goes moooo!\n"; 24*0Sstevel@tonic-gate } 25*0Sstevel@tonic-gate sub Horse::speak { 26*0Sstevel@tonic-gate print "a Horse goes neigh!\n"; 27*0Sstevel@tonic-gate } 28*0Sstevel@tonic-gate sub Sheep::speak { 29*0Sstevel@tonic-gate print "a Sheep goes baaaah!\n" 30*0Sstevel@tonic-gate } 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gate Cow::speak; 33*0Sstevel@tonic-gate Horse::speak; 34*0Sstevel@tonic-gate Sheep::speak; 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gateThis results in: 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate a Cow goes moooo! 39*0Sstevel@tonic-gate a Horse goes neigh! 40*0Sstevel@tonic-gate a Sheep goes baaaah! 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gateNothing spectacular here. Simple subroutines, albeit from separate 43*0Sstevel@tonic-gatepackages, and called using the full package name. So let's create 44*0Sstevel@tonic-gatean entire pasture: 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gate # Cow::speak, Horse::speak, Sheep::speak as before 47*0Sstevel@tonic-gate @pasture = qw(Cow Cow Horse Sheep Sheep); 48*0Sstevel@tonic-gate foreach $animal (@pasture) { 49*0Sstevel@tonic-gate &{$animal."::speak"}; 50*0Sstevel@tonic-gate } 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gateThis results in: 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gate a Cow goes moooo! 55*0Sstevel@tonic-gate a Cow goes moooo! 56*0Sstevel@tonic-gate a Horse goes neigh! 57*0Sstevel@tonic-gate a Sheep goes baaaah! 58*0Sstevel@tonic-gate a Sheep goes baaaah! 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gateWow. That symbolic coderef de-referencing there is pretty nasty. 61*0Sstevel@tonic-gateWe're counting on C<no strict subs> mode, certainly not recommended 62*0Sstevel@tonic-gatefor larger programs. And why was that necessary? Because the name of 63*0Sstevel@tonic-gatethe package seems to be inseparable from the name of the subroutine we 64*0Sstevel@tonic-gatewant to invoke within that package. 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gateOr is it? 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate=head2 Introducing the method invocation arrow 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gateFor now, let's say that C<< Class->method >> invokes subroutine 71*0Sstevel@tonic-gateC<method> in package C<Class>. (Here, "Class" is used in its 72*0Sstevel@tonic-gate"category" meaning, not its "scholastic" meaning.) That's not 73*0Sstevel@tonic-gatecompletely accurate, but we'll do this one step at a time. Now let's 74*0Sstevel@tonic-gateuse it like so: 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate # Cow::speak, Horse::speak, Sheep::speak as before 77*0Sstevel@tonic-gate Cow->speak; 78*0Sstevel@tonic-gate Horse->speak; 79*0Sstevel@tonic-gate Sheep->speak; 80*0Sstevel@tonic-gate 81*0Sstevel@tonic-gateAnd once again, this results in: 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate a Cow goes moooo! 84*0Sstevel@tonic-gate a Horse goes neigh! 85*0Sstevel@tonic-gate a Sheep goes baaaah! 86*0Sstevel@tonic-gate 87*0Sstevel@tonic-gateThat's not fun yet. Same number of characters, all constant, no 88*0Sstevel@tonic-gatevariables. But yet, the parts are separable now. Watch: 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate $a = "Cow"; 91*0Sstevel@tonic-gate $a->speak; # invokes Cow->speak 92*0Sstevel@tonic-gate 93*0Sstevel@tonic-gateAhh! Now that the package name has been parted from the subroutine 94*0Sstevel@tonic-gatename, we can use a variable package name. And this time, we've got 95*0Sstevel@tonic-gatesomething that works even when C<use strict refs> is enabled. 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gate=head2 Invoking a barnyard 98*0Sstevel@tonic-gate 99*0Sstevel@tonic-gateLet's take that new arrow invocation and put it back in the barnyard 100*0Sstevel@tonic-gateexample: 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gate sub Cow::speak { 103*0Sstevel@tonic-gate print "a Cow goes moooo!\n"; 104*0Sstevel@tonic-gate } 105*0Sstevel@tonic-gate sub Horse::speak { 106*0Sstevel@tonic-gate print "a Horse goes neigh!\n"; 107*0Sstevel@tonic-gate } 108*0Sstevel@tonic-gate sub Sheep::speak { 109*0Sstevel@tonic-gate print "a Sheep goes baaaah!\n" 110*0Sstevel@tonic-gate } 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gate @pasture = qw(Cow Cow Horse Sheep Sheep); 113*0Sstevel@tonic-gate foreach $animal (@pasture) { 114*0Sstevel@tonic-gate $animal->speak; 115*0Sstevel@tonic-gate } 116*0Sstevel@tonic-gate 117*0Sstevel@tonic-gateThere! Now we have the animals all talking, and safely at that, 118*0Sstevel@tonic-gatewithout the use of symbolic coderefs. 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateBut look at all that common code. Each of the C<speak> routines has a 121*0Sstevel@tonic-gatesimilar structure: a C<print> operator and a string that contains 122*0Sstevel@tonic-gatecommon text, except for two of the words. It'd be nice if we could 123*0Sstevel@tonic-gatefactor out the commonality, in case we decide later to change it all 124*0Sstevel@tonic-gateto C<says> instead of C<goes>. 125*0Sstevel@tonic-gate 126*0Sstevel@tonic-gateAnd we actually have a way of doing that without much fuss, but we 127*0Sstevel@tonic-gatehave to hear a bit more about what the method invocation arrow is 128*0Sstevel@tonic-gateactually doing for us. 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate=head2 The extra parameter of method invocation 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gateThe invocation of: 133*0Sstevel@tonic-gate 134*0Sstevel@tonic-gate Class->method(@args) 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gateattempts to invoke subroutine C<Class::method> as: 137*0Sstevel@tonic-gate 138*0Sstevel@tonic-gate Class::method("Class", @args); 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gate(If the subroutine can't be found, "inheritance" kicks in, but we'll 141*0Sstevel@tonic-gateget to that later.) This means that we get the class name as the 142*0Sstevel@tonic-gatefirst parameter (the only parameter, if no arguments are given). So 143*0Sstevel@tonic-gatewe can rewrite the C<Sheep> speaking subroutine as: 144*0Sstevel@tonic-gate 145*0Sstevel@tonic-gate sub Sheep::speak { 146*0Sstevel@tonic-gate my $class = shift; 147*0Sstevel@tonic-gate print "a $class goes baaaah!\n"; 148*0Sstevel@tonic-gate } 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gateAnd the other two animals come out similarly: 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate sub Cow::speak { 153*0Sstevel@tonic-gate my $class = shift; 154*0Sstevel@tonic-gate print "a $class goes moooo!\n"; 155*0Sstevel@tonic-gate } 156*0Sstevel@tonic-gate sub Horse::speak { 157*0Sstevel@tonic-gate my $class = shift; 158*0Sstevel@tonic-gate print "a $class goes neigh!\n"; 159*0Sstevel@tonic-gate } 160*0Sstevel@tonic-gate 161*0Sstevel@tonic-gateIn each case, C<$class> will get the value appropriate for that 162*0Sstevel@tonic-gatesubroutine. But once again, we have a lot of similar structure. Can 163*0Sstevel@tonic-gatewe factor that out even further? Yes, by calling another method in 164*0Sstevel@tonic-gatethe same class. 165*0Sstevel@tonic-gate 166*0Sstevel@tonic-gate=head2 Calling a second method to simplify things 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gateLet's call out from C<speak> to a helper method called C<sound>. 169*0Sstevel@tonic-gateThis method provides the constant text for the sound itself. 170*0Sstevel@tonic-gate 171*0Sstevel@tonic-gate { package Cow; 172*0Sstevel@tonic-gate sub sound { "moooo" } 173*0Sstevel@tonic-gate sub speak { 174*0Sstevel@tonic-gate my $class = shift; 175*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n" 176*0Sstevel@tonic-gate } 177*0Sstevel@tonic-gate } 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gateNow, when we call C<< Cow->speak >>, we get a C<$class> of C<Cow> in 180*0Sstevel@tonic-gateC<speak>. This in turn selects the C<< Cow->sound >> method, which 181*0Sstevel@tonic-gatereturns C<moooo>. But how different would this be for the C<Horse>? 182*0Sstevel@tonic-gate 183*0Sstevel@tonic-gate { package Horse; 184*0Sstevel@tonic-gate sub sound { "neigh" } 185*0Sstevel@tonic-gate sub speak { 186*0Sstevel@tonic-gate my $class = shift; 187*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n" 188*0Sstevel@tonic-gate } 189*0Sstevel@tonic-gate } 190*0Sstevel@tonic-gate 191*0Sstevel@tonic-gateOnly the name of the package and the specific sound change. So can we 192*0Sstevel@tonic-gatesomehow share the definition for C<speak> between the Cow and the 193*0Sstevel@tonic-gateHorse? Yes, with inheritance! 194*0Sstevel@tonic-gate 195*0Sstevel@tonic-gate=head2 Inheriting the windpipes 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gateWe'll define a common subroutine package called C<Animal>, with the 198*0Sstevel@tonic-gatedefinition for C<speak>: 199*0Sstevel@tonic-gate 200*0Sstevel@tonic-gate { package Animal; 201*0Sstevel@tonic-gate sub speak { 202*0Sstevel@tonic-gate my $class = shift; 203*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n" 204*0Sstevel@tonic-gate } 205*0Sstevel@tonic-gate } 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gateThen, for each animal, we say it "inherits" from C<Animal>, along 208*0Sstevel@tonic-gatewith the animal-specific sound: 209*0Sstevel@tonic-gate 210*0Sstevel@tonic-gate { package Cow; 211*0Sstevel@tonic-gate @ISA = qw(Animal); 212*0Sstevel@tonic-gate sub sound { "moooo" } 213*0Sstevel@tonic-gate } 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gateNote the added C<@ISA> array. We'll get to that in a minute. 216*0Sstevel@tonic-gate 217*0Sstevel@tonic-gateBut what happens when we invoke C<< Cow->speak >> now? 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gateFirst, Perl constructs the argument list. In this case, it's just 220*0Sstevel@tonic-gateC<Cow>. Then Perl looks for C<Cow::speak>. But that's not there, so 221*0Sstevel@tonic-gatePerl checks for the inheritance array C<@Cow::ISA>. It's there, 222*0Sstevel@tonic-gateand contains the single name C<Animal>. 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gatePerl next checks for C<speak> inside C<Animal> instead, as in 225*0Sstevel@tonic-gateC<Animal::speak>. And that's found, so Perl invokes that subroutine 226*0Sstevel@tonic-gatewith the already frozen argument list. 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gateInside the C<Animal::speak> subroutine, C<$class> becomes C<Cow> (the 229*0Sstevel@tonic-gatefirst argument). So when we get to the step of invoking 230*0Sstevel@tonic-gateC<< $class->sound >>, it'll be looking for C<< Cow->sound >>, which 231*0Sstevel@tonic-gategets it on the first try without looking at C<@ISA>. Success! 232*0Sstevel@tonic-gate 233*0Sstevel@tonic-gate=head2 A few notes about @ISA 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gateThis magical C<@ISA> variable (pronounced "is a" not "ice-uh"), has 236*0Sstevel@tonic-gatedeclared that C<Cow> "is a" C<Animal>. Note that it's an array, 237*0Sstevel@tonic-gatenot a simple single value, because on rare occasions, it makes sense 238*0Sstevel@tonic-gateto have more than one parent class searched for the missing methods. 239*0Sstevel@tonic-gate 240*0Sstevel@tonic-gateIf C<Animal> also had an C<@ISA>, then we'd check there too. The 241*0Sstevel@tonic-gatesearch is recursive, depth-first, left-to-right in each C<@ISA>. 242*0Sstevel@tonic-gateTypically, each C<@ISA> has only one element (multiple elements means 243*0Sstevel@tonic-gatemultiple inheritance and multiple headaches), so we get a nice tree of 244*0Sstevel@tonic-gateinheritance. 245*0Sstevel@tonic-gate 246*0Sstevel@tonic-gateWhen we turn on C<use strict>, we'll get complaints on C<@ISA>, since 247*0Sstevel@tonic-gateit's not a variable containing an explicit package name, nor is it a 248*0Sstevel@tonic-gatelexical ("my") variable. We can't make it a lexical variable though 249*0Sstevel@tonic-gate(it has to belong to the package to be found by the inheritance mechanism), 250*0Sstevel@tonic-gateso there's a couple of straightforward ways to handle that. 251*0Sstevel@tonic-gate 252*0Sstevel@tonic-gateThe easiest is to just spell the package name out: 253*0Sstevel@tonic-gate 254*0Sstevel@tonic-gate @Cow::ISA = qw(Animal); 255*0Sstevel@tonic-gate 256*0Sstevel@tonic-gateOr allow it as an implicitly named package variable: 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gate package Cow; 259*0Sstevel@tonic-gate use vars qw(@ISA); 260*0Sstevel@tonic-gate @ISA = qw(Animal); 261*0Sstevel@tonic-gate 262*0Sstevel@tonic-gateIf you're bringing in the class from outside, via an object-oriented 263*0Sstevel@tonic-gatemodule, you change: 264*0Sstevel@tonic-gate 265*0Sstevel@tonic-gate package Cow; 266*0Sstevel@tonic-gate use Animal; 267*0Sstevel@tonic-gate use vars qw(@ISA); 268*0Sstevel@tonic-gate @ISA = qw(Animal); 269*0Sstevel@tonic-gate 270*0Sstevel@tonic-gateinto just: 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gate package Cow; 273*0Sstevel@tonic-gate use base qw(Animal); 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gateAnd that's pretty darn compact. 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate=head2 Overriding the methods 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gateLet's add a mouse, which can barely be heard: 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate # Animal package from before 282*0Sstevel@tonic-gate { package Mouse; 283*0Sstevel@tonic-gate @ISA = qw(Animal); 284*0Sstevel@tonic-gate sub sound { "squeak" } 285*0Sstevel@tonic-gate sub speak { 286*0Sstevel@tonic-gate my $class = shift; 287*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n"; 288*0Sstevel@tonic-gate print "[but you can barely hear it!]\n"; 289*0Sstevel@tonic-gate } 290*0Sstevel@tonic-gate } 291*0Sstevel@tonic-gate 292*0Sstevel@tonic-gate Mouse->speak; 293*0Sstevel@tonic-gate 294*0Sstevel@tonic-gatewhich results in: 295*0Sstevel@tonic-gate 296*0Sstevel@tonic-gate a Mouse goes squeak! 297*0Sstevel@tonic-gate [but you can barely hear it!] 298*0Sstevel@tonic-gate 299*0Sstevel@tonic-gateHere, C<Mouse> has its own speaking routine, so C<< Mouse->speak >> 300*0Sstevel@tonic-gatedoesn't immediately invoke C<< Animal->speak >>. This is known as 301*0Sstevel@tonic-gate"overriding". In fact, we didn't even need to say that a C<Mouse> was 302*0Sstevel@tonic-gatean C<Animal> at all, since all of the methods needed for C<speak> are 303*0Sstevel@tonic-gatecompletely defined with C<Mouse>. 304*0Sstevel@tonic-gate 305*0Sstevel@tonic-gateBut we've now duplicated some of the code from C<< Animal->speak >>, 306*0Sstevel@tonic-gateand this can once again be a maintenance headache. So, can we avoid 307*0Sstevel@tonic-gatethat? Can we say somehow that a C<Mouse> does everything any other 308*0Sstevel@tonic-gateC<Animal> does, but add in the extra comment? Sure! 309*0Sstevel@tonic-gate 310*0Sstevel@tonic-gateFirst, we can invoke the C<Animal::speak> method directly: 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate # Animal package from before 313*0Sstevel@tonic-gate { package Mouse; 314*0Sstevel@tonic-gate @ISA = qw(Animal); 315*0Sstevel@tonic-gate sub sound { "squeak" } 316*0Sstevel@tonic-gate sub speak { 317*0Sstevel@tonic-gate my $class = shift; 318*0Sstevel@tonic-gate Animal::speak($class); 319*0Sstevel@tonic-gate print "[but you can barely hear it!]\n"; 320*0Sstevel@tonic-gate } 321*0Sstevel@tonic-gate } 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gateNote that we have to include the C<$class> parameter (almost surely 324*0Sstevel@tonic-gatethe value of C<"Mouse">) as the first parameter to C<Animal::speak>, 325*0Sstevel@tonic-gatesince we've stopped using the method arrow. Why did we stop? Well, 326*0Sstevel@tonic-gateif we invoke C<< Animal->speak >> there, the first parameter to the 327*0Sstevel@tonic-gatemethod will be C<"Animal"> not C<"Mouse">, and when time comes for it 328*0Sstevel@tonic-gateto call for the C<sound>, it won't have the right class to come back 329*0Sstevel@tonic-gateto this package. 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gateInvoking C<Animal::speak> directly is a mess, however. What if 332*0Sstevel@tonic-gateC<Animal::speak> didn't exist before, and was being inherited from a 333*0Sstevel@tonic-gateclass mentioned in C<@Animal::ISA>? Because we are no longer using 334*0Sstevel@tonic-gatethe method arrow, we get one and only one chance to hit the right 335*0Sstevel@tonic-gatesubroutine. 336*0Sstevel@tonic-gate 337*0Sstevel@tonic-gateAlso note that the C<Animal> classname is now hardwired into the 338*0Sstevel@tonic-gatesubroutine selection. This is a mess if someone maintains the code, 339*0Sstevel@tonic-gatechanging C<@ISA> for <Mouse> and didn't notice C<Animal> there in 340*0Sstevel@tonic-gateC<speak>. So, this is probably not the right way to go. 341*0Sstevel@tonic-gate 342*0Sstevel@tonic-gate=head2 Starting the search from a different place 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gateA better solution is to tell Perl to search from a higher place 345*0Sstevel@tonic-gatein the inheritance chain: 346*0Sstevel@tonic-gate 347*0Sstevel@tonic-gate # same Animal as before 348*0Sstevel@tonic-gate { package Mouse; 349*0Sstevel@tonic-gate # same @ISA, &sound as before 350*0Sstevel@tonic-gate sub speak { 351*0Sstevel@tonic-gate my $class = shift; 352*0Sstevel@tonic-gate $class->Animal::speak; 353*0Sstevel@tonic-gate print "[but you can barely hear it!]\n"; 354*0Sstevel@tonic-gate } 355*0Sstevel@tonic-gate } 356*0Sstevel@tonic-gate 357*0Sstevel@tonic-gateAhh. This works. Using this syntax, we start with C<Animal> to find 358*0Sstevel@tonic-gateC<speak>, and use all of C<Animal>'s inheritance chain if not found 359*0Sstevel@tonic-gateimmediately. And yet the first parameter will be C<$class>, so the 360*0Sstevel@tonic-gatefound C<speak> method will get C<Mouse> as its first entry, and 361*0Sstevel@tonic-gateeventually work its way back to C<Mouse::sound> for the details. 362*0Sstevel@tonic-gate 363*0Sstevel@tonic-gateBut this isn't the best solution. We still have to keep the C<@ISA> 364*0Sstevel@tonic-gateand the initial search package coordinated. Worse, if C<Mouse> had 365*0Sstevel@tonic-gatemultiple entries in C<@ISA>, we wouldn't necessarily know which one 366*0Sstevel@tonic-gatehad actually defined C<speak>. So, is there an even better way? 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate=head2 The SUPER way of doing things 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gateBy changing the C<Animal> class to the C<SUPER> class in that 371*0Sstevel@tonic-gateinvocation, we get a search of all of our super classes (classes 372*0Sstevel@tonic-gatelisted in C<@ISA>) automatically: 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate # same Animal as before 375*0Sstevel@tonic-gate { package Mouse; 376*0Sstevel@tonic-gate # same @ISA, &sound as before 377*0Sstevel@tonic-gate sub speak { 378*0Sstevel@tonic-gate my $class = shift; 379*0Sstevel@tonic-gate $class->SUPER::speak; 380*0Sstevel@tonic-gate print "[but you can barely hear it!]\n"; 381*0Sstevel@tonic-gate } 382*0Sstevel@tonic-gate } 383*0Sstevel@tonic-gate 384*0Sstevel@tonic-gateSo, C<SUPER::speak> means look in the current package's C<@ISA> for 385*0Sstevel@tonic-gateC<speak>, invoking the first one found. Note that it does I<not> look in 386*0Sstevel@tonic-gatethe C<@ISA> of C<$class>. 387*0Sstevel@tonic-gate 388*0Sstevel@tonic-gate=head2 Where we're at so far... 389*0Sstevel@tonic-gate 390*0Sstevel@tonic-gateSo far, we've seen the method arrow syntax: 391*0Sstevel@tonic-gate 392*0Sstevel@tonic-gate Class->method(@args); 393*0Sstevel@tonic-gate 394*0Sstevel@tonic-gateor the equivalent: 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate $a = "Class"; 397*0Sstevel@tonic-gate $a->method(@args); 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gatewhich constructs an argument list of: 400*0Sstevel@tonic-gate 401*0Sstevel@tonic-gate ("Class", @args) 402*0Sstevel@tonic-gate 403*0Sstevel@tonic-gateand attempts to invoke 404*0Sstevel@tonic-gate 405*0Sstevel@tonic-gate Class::method("Class", @Args); 406*0Sstevel@tonic-gate 407*0Sstevel@tonic-gateHowever, if C<Class::method> is not found, then C<@Class::ISA> is examined 408*0Sstevel@tonic-gate(recursively) to locate a package that does indeed contain C<method>, 409*0Sstevel@tonic-gateand that subroutine is invoked instead. 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gateUsing this simple syntax, we have class methods, (multiple) 412*0Sstevel@tonic-gateinheritance, overriding, and extending. Using just what we've seen so 413*0Sstevel@tonic-gatefar, we've been able to factor out common code, and provide a nice way 414*0Sstevel@tonic-gateto reuse implementations with variations. This is at the core of what 415*0Sstevel@tonic-gateobjects provide, but objects also provide instance data, which we 416*0Sstevel@tonic-gatehaven't even begun to cover. 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate=head2 A horse is a horse, of course of course -- or is it? 419*0Sstevel@tonic-gate 420*0Sstevel@tonic-gateLet's start with the code for the C<Animal> class 421*0Sstevel@tonic-gateand the C<Horse> class: 422*0Sstevel@tonic-gate 423*0Sstevel@tonic-gate { package Animal; 424*0Sstevel@tonic-gate sub speak { 425*0Sstevel@tonic-gate my $class = shift; 426*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n" 427*0Sstevel@tonic-gate } 428*0Sstevel@tonic-gate } 429*0Sstevel@tonic-gate { package Horse; 430*0Sstevel@tonic-gate @ISA = qw(Animal); 431*0Sstevel@tonic-gate sub sound { "neigh" } 432*0Sstevel@tonic-gate } 433*0Sstevel@tonic-gate 434*0Sstevel@tonic-gateThis lets us invoke C<< Horse->speak >> to ripple upward to 435*0Sstevel@tonic-gateC<Animal::speak>, calling back to C<Horse::sound> to get the specific 436*0Sstevel@tonic-gatesound, and the output of: 437*0Sstevel@tonic-gate 438*0Sstevel@tonic-gate a Horse goes neigh! 439*0Sstevel@tonic-gate 440*0Sstevel@tonic-gateBut all of our Horse objects would have to be absolutely identical. 441*0Sstevel@tonic-gateIf I add a subroutine, all horses automatically share it. That's 442*0Sstevel@tonic-gategreat for making horses the same, but how do we capture the 443*0Sstevel@tonic-gatedistinctions about an individual horse? For example, suppose I want 444*0Sstevel@tonic-gateto give my first horse a name. There's got to be a way to keep its 445*0Sstevel@tonic-gatename separate from the other horses. 446*0Sstevel@tonic-gate 447*0Sstevel@tonic-gateWe can do that by drawing a new distinction, called an "instance". 448*0Sstevel@tonic-gateAn "instance" is generally created by a class. In Perl, any reference 449*0Sstevel@tonic-gatecan be an instance, so let's start with the simplest reference 450*0Sstevel@tonic-gatethat can hold a horse's name: a scalar reference. 451*0Sstevel@tonic-gate 452*0Sstevel@tonic-gate my $name = "Mr. Ed"; 453*0Sstevel@tonic-gate my $talking = \$name; 454*0Sstevel@tonic-gate 455*0Sstevel@tonic-gateSo now C<$talking> is a reference to what will be the instance-specific 456*0Sstevel@tonic-gatedata (the name). The final step in turning this into a real instance 457*0Sstevel@tonic-gateis with a special operator called C<bless>: 458*0Sstevel@tonic-gate 459*0Sstevel@tonic-gate bless $talking, Horse; 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gateThis operator stores information about the package named C<Horse> into 462*0Sstevel@tonic-gatethe thing pointed at by the reference. At this point, we say 463*0Sstevel@tonic-gateC<$talking> is an instance of C<Horse>. That is, it's a specific 464*0Sstevel@tonic-gatehorse. The reference is otherwise unchanged, and can still be used 465*0Sstevel@tonic-gatewith traditional dereferencing operators. 466*0Sstevel@tonic-gate 467*0Sstevel@tonic-gate=head2 Invoking an instance method 468*0Sstevel@tonic-gate 469*0Sstevel@tonic-gateThe method arrow can be used on instances, as well as names of 470*0Sstevel@tonic-gatepackages (classes). So, let's get the sound that C<$talking> makes: 471*0Sstevel@tonic-gate 472*0Sstevel@tonic-gate my $noise = $talking->sound; 473*0Sstevel@tonic-gate 474*0Sstevel@tonic-gateTo invoke C<sound>, Perl first notes that C<$talking> is a blessed 475*0Sstevel@tonic-gatereference (and thus an instance). It then constructs an argument 476*0Sstevel@tonic-gatelist, in this case from just C<($talking)>. (Later we'll see that 477*0Sstevel@tonic-gatearguments will take their place following the instance variable, 478*0Sstevel@tonic-gatejust like with classes.) 479*0Sstevel@tonic-gate 480*0Sstevel@tonic-gateNow for the fun part: Perl takes the class in which the instance was 481*0Sstevel@tonic-gateblessed, in this case C<Horse>, and uses that to locate the subroutine 482*0Sstevel@tonic-gateto invoke the method. In this case, C<Horse::sound> is found directly 483*0Sstevel@tonic-gate(without using inheritance), yielding the final subroutine invocation: 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate Horse::sound($talking) 486*0Sstevel@tonic-gate 487*0Sstevel@tonic-gateNote that the first parameter here is still the instance, not the name 488*0Sstevel@tonic-gateof the class as before. We'll get C<neigh> as the return value, and 489*0Sstevel@tonic-gatethat'll end up as the C<$noise> variable above. 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gateIf Horse::sound had not been found, we'd be wandering up the 492*0Sstevel@tonic-gateC<@Horse::ISA> list to try to find the method in one of the 493*0Sstevel@tonic-gatesuperclasses, just as for a class method. The only difference between 494*0Sstevel@tonic-gatea class method and an instance method is whether the first parameter 495*0Sstevel@tonic-gateis an instance (a blessed reference) or a class name (a string). 496*0Sstevel@tonic-gate 497*0Sstevel@tonic-gate=head2 Accessing the instance data 498*0Sstevel@tonic-gate 499*0Sstevel@tonic-gateBecause we get the instance as the first parameter, we can now access 500*0Sstevel@tonic-gatethe instance-specific data. In this case, let's add a way to get at 501*0Sstevel@tonic-gatethe name: 502*0Sstevel@tonic-gate 503*0Sstevel@tonic-gate { package Horse; 504*0Sstevel@tonic-gate @ISA = qw(Animal); 505*0Sstevel@tonic-gate sub sound { "neigh" } 506*0Sstevel@tonic-gate sub name { 507*0Sstevel@tonic-gate my $self = shift; 508*0Sstevel@tonic-gate $$self; 509*0Sstevel@tonic-gate } 510*0Sstevel@tonic-gate } 511*0Sstevel@tonic-gate 512*0Sstevel@tonic-gateNow we call for the name: 513*0Sstevel@tonic-gate 514*0Sstevel@tonic-gate print $talking->name, " says ", $talking->sound, "\n"; 515*0Sstevel@tonic-gate 516*0Sstevel@tonic-gateInside C<Horse::name>, the C<@_> array contains just C<$talking>, 517*0Sstevel@tonic-gatewhich the C<shift> stores into C<$self>. (It's traditional to shift 518*0Sstevel@tonic-gatethe first parameter off into a variable named C<$self> for instance 519*0Sstevel@tonic-gatemethods, so stay with that unless you have strong reasons otherwise.) 520*0Sstevel@tonic-gateThen, C<$self> gets de-referenced as a scalar ref, yielding C<Mr. Ed>, 521*0Sstevel@tonic-gateand we're done with that. The result is: 522*0Sstevel@tonic-gate 523*0Sstevel@tonic-gate Mr. Ed says neigh. 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate=head2 How to build a horse 526*0Sstevel@tonic-gate 527*0Sstevel@tonic-gateOf course, if we constructed all of our horses by hand, we'd most 528*0Sstevel@tonic-gatelikely make mistakes from time to time. We're also violating one of 529*0Sstevel@tonic-gatethe properties of object-oriented programming, in that the "inside 530*0Sstevel@tonic-gateguts" of a Horse are visible. That's good if you're a veterinarian, 531*0Sstevel@tonic-gatebut not if you just like to own horses. So, let's let the Horse class 532*0Sstevel@tonic-gatebuild a new horse: 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate { package Horse; 535*0Sstevel@tonic-gate @ISA = qw(Animal); 536*0Sstevel@tonic-gate sub sound { "neigh" } 537*0Sstevel@tonic-gate sub name { 538*0Sstevel@tonic-gate my $self = shift; 539*0Sstevel@tonic-gate $$self; 540*0Sstevel@tonic-gate } 541*0Sstevel@tonic-gate sub named { 542*0Sstevel@tonic-gate my $class = shift; 543*0Sstevel@tonic-gate my $name = shift; 544*0Sstevel@tonic-gate bless \$name, $class; 545*0Sstevel@tonic-gate } 546*0Sstevel@tonic-gate } 547*0Sstevel@tonic-gate 548*0Sstevel@tonic-gateNow with the new C<named> method, we can build a horse: 549*0Sstevel@tonic-gate 550*0Sstevel@tonic-gate my $talking = Horse->named("Mr. Ed"); 551*0Sstevel@tonic-gate 552*0Sstevel@tonic-gateNotice we're back to a class method, so the two arguments to 553*0Sstevel@tonic-gateC<Horse::named> are C<Horse> and C<Mr. Ed>. The C<bless> operator 554*0Sstevel@tonic-gatenot only blesses C<$name>, it also returns the reference to C<$name>, 555*0Sstevel@tonic-gateso that's fine as a return value. And that's how to build a horse. 556*0Sstevel@tonic-gate 557*0Sstevel@tonic-gateWe've called the constructor C<named> here, so that it quickly denotes 558*0Sstevel@tonic-gatethe constructor's argument as the name for this particular C<Horse>. 559*0Sstevel@tonic-gateYou can use different constructors with different names for different 560*0Sstevel@tonic-gateways of "giving birth" to the object (like maybe recording its 561*0Sstevel@tonic-gatepedigree or date of birth). However, you'll find that most people 562*0Sstevel@tonic-gatecoming to Perl from more limited languages use a single constructor 563*0Sstevel@tonic-gatenamed C<new>, with various ways of interpreting the arguments to 564*0Sstevel@tonic-gateC<new>. Either style is fine, as long as you document your particular 565*0Sstevel@tonic-gateway of giving birth to an object. (And you I<were> going to do that, 566*0Sstevel@tonic-gateright?) 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gate=head2 Inheriting the constructor 569*0Sstevel@tonic-gate 570*0Sstevel@tonic-gateBut was there anything specific to C<Horse> in that method? No. Therefore, 571*0Sstevel@tonic-gateit's also the same recipe for building anything else that inherited from 572*0Sstevel@tonic-gateC<Animal>, so let's put it there: 573*0Sstevel@tonic-gate 574*0Sstevel@tonic-gate { package Animal; 575*0Sstevel@tonic-gate sub speak { 576*0Sstevel@tonic-gate my $class = shift; 577*0Sstevel@tonic-gate print "a $class goes ", $class->sound, "!\n" 578*0Sstevel@tonic-gate } 579*0Sstevel@tonic-gate sub name { 580*0Sstevel@tonic-gate my $self = shift; 581*0Sstevel@tonic-gate $$self; 582*0Sstevel@tonic-gate } 583*0Sstevel@tonic-gate sub named { 584*0Sstevel@tonic-gate my $class = shift; 585*0Sstevel@tonic-gate my $name = shift; 586*0Sstevel@tonic-gate bless \$name, $class; 587*0Sstevel@tonic-gate } 588*0Sstevel@tonic-gate } 589*0Sstevel@tonic-gate { package Horse; 590*0Sstevel@tonic-gate @ISA = qw(Animal); 591*0Sstevel@tonic-gate sub sound { "neigh" } 592*0Sstevel@tonic-gate } 593*0Sstevel@tonic-gate 594*0Sstevel@tonic-gateAhh, but what happens if we invoke C<speak> on an instance? 595*0Sstevel@tonic-gate 596*0Sstevel@tonic-gate my $talking = Horse->named("Mr. Ed"); 597*0Sstevel@tonic-gate $talking->speak; 598*0Sstevel@tonic-gate 599*0Sstevel@tonic-gateWe get a debugging value: 600*0Sstevel@tonic-gate 601*0Sstevel@tonic-gate a Horse=SCALAR(0xaca42ac) goes neigh! 602*0Sstevel@tonic-gate 603*0Sstevel@tonic-gateWhy? Because the C<Animal::speak> routine is expecting a classname as 604*0Sstevel@tonic-gateits first parameter, not an instance. When the instance is passed in, 605*0Sstevel@tonic-gatewe'll end up using a blessed scalar reference as a string, and that 606*0Sstevel@tonic-gateshows up as we saw it just now. 607*0Sstevel@tonic-gate 608*0Sstevel@tonic-gate=head2 Making a method work with either classes or instances 609*0Sstevel@tonic-gate 610*0Sstevel@tonic-gateAll we need is for a method to detect if it is being called on a class 611*0Sstevel@tonic-gateor called on an instance. The most straightforward way is with the 612*0Sstevel@tonic-gateC<ref> operator. This returns a string (the classname) when used on a 613*0Sstevel@tonic-gateblessed reference, and C<undef> when used on a string (like a 614*0Sstevel@tonic-gateclassname). Let's modify the C<name> method first to notice the change: 615*0Sstevel@tonic-gate 616*0Sstevel@tonic-gate sub name { 617*0Sstevel@tonic-gate my $either = shift; 618*0Sstevel@tonic-gate ref $either 619*0Sstevel@tonic-gate ? $$either # it's an instance, return name 620*0Sstevel@tonic-gate : "an unnamed $either"; # it's a class, return generic 621*0Sstevel@tonic-gate } 622*0Sstevel@tonic-gate 623*0Sstevel@tonic-gateHere, the C<?:> operator comes in handy to select either the 624*0Sstevel@tonic-gatedereference or a derived string. Now we can use this with either an 625*0Sstevel@tonic-gateinstance or a class. Note that I've changed the first parameter 626*0Sstevel@tonic-gateholder to C<$either> to show that this is intended: 627*0Sstevel@tonic-gate 628*0Sstevel@tonic-gate my $talking = Horse->named("Mr. Ed"); 629*0Sstevel@tonic-gate print Horse->name, "\n"; # prints "an unnamed Horse\n" 630*0Sstevel@tonic-gate print $talking->name, "\n"; # prints "Mr Ed.\n" 631*0Sstevel@tonic-gate 632*0Sstevel@tonic-gateand now we'll fix C<speak> to use this: 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate sub speak { 635*0Sstevel@tonic-gate my $either = shift; 636*0Sstevel@tonic-gate print $either->name, " goes ", $either->sound, "\n"; 637*0Sstevel@tonic-gate } 638*0Sstevel@tonic-gate 639*0Sstevel@tonic-gateAnd since C<sound> already worked with either a class or an instance, 640*0Sstevel@tonic-gatewe're done! 641*0Sstevel@tonic-gate 642*0Sstevel@tonic-gate=head2 Adding parameters to a method 643*0Sstevel@tonic-gate 644*0Sstevel@tonic-gateLet's train our animals to eat: 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gate { package Animal; 647*0Sstevel@tonic-gate sub named { 648*0Sstevel@tonic-gate my $class = shift; 649*0Sstevel@tonic-gate my $name = shift; 650*0Sstevel@tonic-gate bless \$name, $class; 651*0Sstevel@tonic-gate } 652*0Sstevel@tonic-gate sub name { 653*0Sstevel@tonic-gate my $either = shift; 654*0Sstevel@tonic-gate ref $either 655*0Sstevel@tonic-gate ? $$either # it's an instance, return name 656*0Sstevel@tonic-gate : "an unnamed $either"; # it's a class, return generic 657*0Sstevel@tonic-gate } 658*0Sstevel@tonic-gate sub speak { 659*0Sstevel@tonic-gate my $either = shift; 660*0Sstevel@tonic-gate print $either->name, " goes ", $either->sound, "\n"; 661*0Sstevel@tonic-gate } 662*0Sstevel@tonic-gate sub eat { 663*0Sstevel@tonic-gate my $either = shift; 664*0Sstevel@tonic-gate my $food = shift; 665*0Sstevel@tonic-gate print $either->name, " eats $food.\n"; 666*0Sstevel@tonic-gate } 667*0Sstevel@tonic-gate } 668*0Sstevel@tonic-gate { package Horse; 669*0Sstevel@tonic-gate @ISA = qw(Animal); 670*0Sstevel@tonic-gate sub sound { "neigh" } 671*0Sstevel@tonic-gate } 672*0Sstevel@tonic-gate { package Sheep; 673*0Sstevel@tonic-gate @ISA = qw(Animal); 674*0Sstevel@tonic-gate sub sound { "baaaah" } 675*0Sstevel@tonic-gate } 676*0Sstevel@tonic-gate 677*0Sstevel@tonic-gateAnd now try it out: 678*0Sstevel@tonic-gate 679*0Sstevel@tonic-gate my $talking = Horse->named("Mr. Ed"); 680*0Sstevel@tonic-gate $talking->eat("hay"); 681*0Sstevel@tonic-gate Sheep->eat("grass"); 682*0Sstevel@tonic-gate 683*0Sstevel@tonic-gatewhich prints: 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gate Mr. Ed eats hay. 686*0Sstevel@tonic-gate an unnamed Sheep eats grass. 687*0Sstevel@tonic-gate 688*0Sstevel@tonic-gateAn instance method with parameters gets invoked with the instance, 689*0Sstevel@tonic-gateand then the list of parameters. So that first invocation is like: 690*0Sstevel@tonic-gate 691*0Sstevel@tonic-gate Animal::eat($talking, "hay"); 692*0Sstevel@tonic-gate 693*0Sstevel@tonic-gate=head2 More interesting instances 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gateWhat if an instance needs more data? Most interesting instances are 696*0Sstevel@tonic-gatemade of many items, each of which can in turn be a reference or even 697*0Sstevel@tonic-gateanother object. The easiest way to store these is often in a hash. 698*0Sstevel@tonic-gateThe keys of the hash serve as the names of parts of the object (often 699*0Sstevel@tonic-gatecalled "instance variables" or "member variables"), and the 700*0Sstevel@tonic-gatecorresponding values are, well, the values. 701*0Sstevel@tonic-gate 702*0Sstevel@tonic-gateBut how do we turn the horse into a hash? Recall that an object was 703*0Sstevel@tonic-gateany blessed reference. We can just as easily make it a blessed hash 704*0Sstevel@tonic-gatereference as a blessed scalar reference, as long as everything that 705*0Sstevel@tonic-gatelooks at the reference is changed accordingly. 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gateLet's make a sheep that has a name and a color: 708*0Sstevel@tonic-gate 709*0Sstevel@tonic-gate my $bad = bless { Name => "Evil", Color => "black" }, Sheep; 710*0Sstevel@tonic-gate 711*0Sstevel@tonic-gateso C<< $bad->{Name} >> has C<Evil>, and C<< $bad->{Color} >> has 712*0Sstevel@tonic-gateC<black>. But we want to make C<< $bad->name >> access the name, and 713*0Sstevel@tonic-gatethat's now messed up because it's expecting a scalar reference. Not 714*0Sstevel@tonic-gateto worry, because that's pretty easy to fix up: 715*0Sstevel@tonic-gate 716*0Sstevel@tonic-gate ## in Animal 717*0Sstevel@tonic-gate sub name { 718*0Sstevel@tonic-gate my $either = shift; 719*0Sstevel@tonic-gate ref $either ? 720*0Sstevel@tonic-gate $either->{Name} : 721*0Sstevel@tonic-gate "an unnamed $either"; 722*0Sstevel@tonic-gate } 723*0Sstevel@tonic-gate 724*0Sstevel@tonic-gateAnd of course C<named> still builds a scalar sheep, so let's fix that 725*0Sstevel@tonic-gateas well: 726*0Sstevel@tonic-gate 727*0Sstevel@tonic-gate ## in Animal 728*0Sstevel@tonic-gate sub named { 729*0Sstevel@tonic-gate my $class = shift; 730*0Sstevel@tonic-gate my $name = shift; 731*0Sstevel@tonic-gate my $self = { Name => $name, Color => $class->default_color }; 732*0Sstevel@tonic-gate bless $self, $class; 733*0Sstevel@tonic-gate } 734*0Sstevel@tonic-gate 735*0Sstevel@tonic-gateWhat's this C<default_color>? Well, if C<named> has only the name, 736*0Sstevel@tonic-gatewe still need to set a color, so we'll have a class-specific initial color. 737*0Sstevel@tonic-gateFor a sheep, we might define it as white: 738*0Sstevel@tonic-gate 739*0Sstevel@tonic-gate ## in Sheep 740*0Sstevel@tonic-gate sub default_color { "white" } 741*0Sstevel@tonic-gate 742*0Sstevel@tonic-gateAnd then to keep from having to define one for each additional class, 743*0Sstevel@tonic-gatewe'll define a "backstop" method that serves as the "default default", 744*0Sstevel@tonic-gatedirectly in C<Animal>: 745*0Sstevel@tonic-gate 746*0Sstevel@tonic-gate ## in Animal 747*0Sstevel@tonic-gate sub default_color { "brown" } 748*0Sstevel@tonic-gate 749*0Sstevel@tonic-gateNow, because C<name> and C<named> were the only methods that 750*0Sstevel@tonic-gatereferenced the "structure" of the object, the rest of the methods can 751*0Sstevel@tonic-gateremain the same, so C<speak> still works as before. 752*0Sstevel@tonic-gate 753*0Sstevel@tonic-gate=head2 A horse of a different color 754*0Sstevel@tonic-gate 755*0Sstevel@tonic-gateBut having all our horses be brown would be boring. So let's add a 756*0Sstevel@tonic-gatemethod or two to get and set the color. 757*0Sstevel@tonic-gate 758*0Sstevel@tonic-gate ## in Animal 759*0Sstevel@tonic-gate sub color { 760*0Sstevel@tonic-gate $_[0]->{Color} 761*0Sstevel@tonic-gate } 762*0Sstevel@tonic-gate sub set_color { 763*0Sstevel@tonic-gate $_[0]->{Color} = $_[1]; 764*0Sstevel@tonic-gate } 765*0Sstevel@tonic-gate 766*0Sstevel@tonic-gateNote the alternate way of accessing the arguments: C<$_[0]> is used 767*0Sstevel@tonic-gatein-place, rather than with a C<shift>. (This saves us a bit of time 768*0Sstevel@tonic-gatefor something that may be invoked frequently.) And now we can fix 769*0Sstevel@tonic-gatethat color for Mr. Ed: 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gate my $talking = Horse->named("Mr. Ed"); 772*0Sstevel@tonic-gate $talking->set_color("black-and-white"); 773*0Sstevel@tonic-gate print $talking->name, " is colored ", $talking->color, "\n"; 774*0Sstevel@tonic-gate 775*0Sstevel@tonic-gatewhich results in: 776*0Sstevel@tonic-gate 777*0Sstevel@tonic-gate Mr. Ed is colored black-and-white 778*0Sstevel@tonic-gate 779*0Sstevel@tonic-gate=head2 Summary 780*0Sstevel@tonic-gate 781*0Sstevel@tonic-gateSo, now we have class methods, constructors, instance methods, 782*0Sstevel@tonic-gateinstance data, and even accessors. But that's still just the 783*0Sstevel@tonic-gatebeginning of what Perl has to offer. We haven't even begun to talk 784*0Sstevel@tonic-gateabout accessors that double as getters and setters, destructors, 785*0Sstevel@tonic-gateindirect object notation, subclasses that add instance data, per-class 786*0Sstevel@tonic-gatedata, overloading, "isa" and "can" tests, C<UNIVERSAL> class, and so 787*0Sstevel@tonic-gateon. That's for the rest of the Perl documentation to cover. 788*0Sstevel@tonic-gateHopefully, this gets you started, though. 789*0Sstevel@tonic-gate 790*0Sstevel@tonic-gate=head1 SEE ALSO 791*0Sstevel@tonic-gate 792*0Sstevel@tonic-gateFor more information, see L<perlobj> (for all the gritty details about 793*0Sstevel@tonic-gatePerl objects, now that you've seen the basics), L<perltoot> (the 794*0Sstevel@tonic-gatetutorial for those who already know objects), L<perltooc> (dealing 795*0Sstevel@tonic-gatewith class data), L<perlbot> (for some more tricks), and books such as 796*0Sstevel@tonic-gateDamian Conway's excellent I<Object Oriented Perl>. 797*0Sstevel@tonic-gate 798*0Sstevel@tonic-gateSome modules which might prove interesting are Class::Accessor, 799*0Sstevel@tonic-gateClass::Class, Class::Contract, Class::Data::Inheritable, 800*0Sstevel@tonic-gateClass::MethodMaker and Tie::SecureHash 801*0Sstevel@tonic-gate 802*0Sstevel@tonic-gate=head1 COPYRIGHT 803*0Sstevel@tonic-gate 804*0Sstevel@tonic-gateCopyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge 805*0Sstevel@tonic-gateConsulting Services, Inc. Permission is hereby granted to distribute 806*0Sstevel@tonic-gatethis document intact with the Perl distribution, and in accordance 807*0Sstevel@tonic-gatewith the licenses of the Perl distribution; derived documents must 808*0Sstevel@tonic-gateinclude this copyright notice intact. 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gatePortions of this text have been derived from Perl Training materials 811*0Sstevel@tonic-gateoriginally appearing in the I<Packages, References, Objects, and 812*0Sstevel@tonic-gateModules> course taught by instructors for Stonehenge Consulting 813*0Sstevel@tonic-gateServices, Inc. and used with permission. 814*0Sstevel@tonic-gate 815*0Sstevel@tonic-gatePortions of this text have been derived from materials originally 816*0Sstevel@tonic-gateappearing in I<Linux Magazine> and used with permission. 817