3 perlboot - Beginner's Object-Oriented Tutorial
7 If you're not familiar with objects from other languages, some of the
8 other Perl object documentation may be a little daunting, such as
9 L<perlobj>, a basic reference in using objects, and L<perltoot>, which
10 introduces readers to the peculiarities of Perl's object system in a
13 So, let's take a different approach, presuming no prior object
14 experience. It helps if you know about subroutines (L<perlsub>),
15 references (L<perlref> et. seq.), and packages (L<perlmod>), so become
16 familiar with those first if you haven't already.
18 =head2 If we could talk to the animals...
20 Let's let the animals talk for a moment:
23 print "a Cow goes moooo!\n";
26 print "a Horse goes neigh!\n";
29 print "a Sheep goes baaaah!\n"
42 Nothing spectacular here. Simple subroutines, albeit from separate
43 packages, and called using the full package name. So let's create
46 # Cow::speak, Horse::speak, Sheep::speak as before
47 @pasture = qw(Cow Cow Horse Sheep Sheep);
48 foreach $animal (@pasture) {
60 Wow. That symbolic coderef de-referencing there is pretty nasty.
61 We're counting on C<no strict subs> mode, certainly not recommended
62 for larger programs. And why was that necessary? Because the name of
63 the package seems to be inseparable from the name of the subroutine we
64 want to invoke within that package.
68 =head2 Introducing the method invocation arrow
70 For now, let's say that C<< Class->method >> invokes subroutine
71 C<method> in package C<Class>. (Here, "Class" is used in its
72 "category" meaning, not its "scholastic" meaning.) That's not
73 completely accurate, but we'll do this one step at a time. Now let's
76 # Cow::speak, Horse::speak, Sheep::speak as before
81 And once again, this results in:
87 That's not fun yet. Same number of characters, all constant, no
88 variables. But yet, the parts are separable now. Watch:
91 $a->speak; # invokes Cow->speak
93 Ahh! Now that the package name has been parted from the subroutine
94 name, we can use a variable package name. And this time, we've got
95 something that works even when C<use strict refs> is enabled.
97 =head2 Invoking a barnyard
99 Let's take that new arrow invocation and put it back in the barnyard
103 print "a Cow goes moooo!\n";
106 print "a Horse goes neigh!\n";
109 print "a Sheep goes baaaah!\n"
112 @pasture = qw(Cow Cow Horse Sheep Sheep);
113 foreach $animal (@pasture) {
117 There! Now we have the animals all talking, and safely at that,
118 without the use of symbolic coderefs.
120 But look at all that common code. Each of the C<speak> routines has a
121 similar structure: a C<print> operator and a string that contains
122 common text, except for two of the words. It'd be nice if we could
123 factor out the commonality, in case we decide later to change it all
124 to C<says> instead of C<goes>.
126 And we actually have a way of doing that without much fuss, but we
127 have to hear a bit more about what the method invocation arrow is
128 actually doing for us.
130 =head2 The extra parameter of method invocation
136 attempts to invoke subroutine C<Class::method> as:
138 Class::method("Class", @args);
140 (If the subroutine can't be found, "inheritance" kicks in, but we'll
141 get to that later.) This means that we get the class name as the
142 first parameter (the only parameter, if no arguments are given). So
143 we can rewrite the C<Sheep> speaking subroutine as:
147 print "a $class goes baaaah!\n";
150 And the other two animals come out similarly:
154 print "a $class goes moooo!\n";
158 print "a $class goes neigh!\n";
161 In each case, C<$class> will get the value appropriate for that
162 subroutine. But once again, we have a lot of similar structure. Can
163 we factor that out even further? Yes, by calling another method in
166 =head2 Calling a second method to simplify things
168 Let's call out from C<speak> to a helper method called C<sound>.
169 This method provides the constant text for the sound itself.
172 sub sound { "moooo" }
175 print "a $class goes ", $class->sound, "!\n"
179 Now, when we call C<< Cow->speak >>, we get a C<$class> of C<Cow> in
180 C<speak>. This in turn selects the C<< Cow->sound >> method, which
181 returns C<moooo>. But how different would this be for the C<Horse>?
184 sub sound { "neigh" }
187 print "a $class goes ", $class->sound, "!\n"
191 Only the name of the package and the specific sound change. So can we
192 somehow share the definition for C<speak> between the Cow and the
193 Horse? Yes, with inheritance!
195 =head2 Inheriting the windpipes
197 We'll define a common subroutine package called C<Animal>, with the
198 definition for C<speak>:
203 print "a $class goes ", $class->sound, "!\n"
207 Then, for each animal, we say it "inherits" from C<Animal>, along
208 with the animal-specific sound:
212 sub sound { "moooo" }
215 Note the added C<@ISA> array. We'll get to that in a minute.
217 But what happens when we invoke C<< Cow->speak >> now?
219 First, Perl constructs the argument list. In this case, it's just
220 C<Cow>. Then Perl looks for C<Cow::speak>. But that's not there, so
221 Perl checks for the inheritance array C<@Cow::ISA>. It's there,
222 and contains the single name C<Animal>.
224 Perl next checks for C<speak> inside C<Animal> instead, as in
225 C<Animal::speak>. And that's found, so Perl invokes that subroutine
226 with the already frozen argument list.
228 Inside the C<Animal::speak> subroutine, C<$class> becomes C<Cow> (the
229 first argument). So when we get to the step of invoking
230 C<< $class->sound >>, it'll be looking for C<< Cow->sound >>, which
231 gets it on the first try without looking at C<@ISA>. Success!
233 =head2 A few notes about @ISA
235 This magical C<@ISA> variable (pronounced "is a" not "ice-uh"), has
236 declared that C<Cow> "is a" C<Animal>. Note that it's an array,
237 not a simple single value, because on rare occasions, it makes sense
238 to have more than one parent class searched for the missing methods.
240 If C<Animal> also had an C<@ISA>, then we'd check there too. The
241 search is recursive, depth-first, left-to-right in each C<@ISA> by
242 default (see L<mro> for alternatives). Typically, each C<@ISA> has
243 only one element (multiple elements means multiple inheritance and
244 multiple headaches), so we get a nice tree of inheritance.
246 When we turn on C<use strict>, we'll get complaints on C<@ISA>, since
247 it's not a variable containing an explicit package name, nor is it a
248 lexical ("my") variable. We can't make it a lexical variable though
249 (it has to belong to the package to be found by the inheritance mechanism),
250 so there's a couple of straightforward ways to handle that.
252 The easiest is to just spell the package name out:
254 @Cow::ISA = qw(Animal);
256 Or allow it as an implicitly named package variable:
262 If you're bringing in the class from outside, via an object-oriented
275 And that's pretty darn compact.
277 =head2 Overriding the methods
279 Let's add a mouse, which can barely be heard:
281 # Animal package from before
284 sub sound { "squeak" }
287 print "a $class goes ", $class->sound, "!\n";
288 print "[but you can barely hear it!]\n";
297 [but you can barely hear it!]
299 Here, C<Mouse> has its own speaking routine, so C<< Mouse->speak >>
300 doesn't immediately invoke C<< Animal->speak >>. This is known as
301 "overriding". In fact, we didn't even need to say that a C<Mouse> was
302 an C<Animal> at all, since all of the methods needed for C<speak> are
303 completely defined with C<Mouse>.
305 But we've now duplicated some of the code from C<< Animal->speak >>,
306 and this can once again be a maintenance headache. So, can we avoid
307 that? Can we say somehow that a C<Mouse> does everything any other
308 C<Animal> does, but add in the extra comment? Sure!
310 First, we can invoke the C<Animal::speak> method directly:
312 # Animal package from before
315 sub sound { "squeak" }
318 Animal::speak($class);
319 print "[but you can barely hear it!]\n";
323 Note that we have to include the C<$class> parameter (almost surely
324 the value of C<"Mouse">) as the first parameter to C<Animal::speak>,
325 since we've stopped using the method arrow. Why did we stop? Well,
326 if we invoke C<< Animal->speak >> there, the first parameter to the
327 method will be C<"Animal"> not C<"Mouse">, and when time comes for it
328 to call for the C<sound>, it won't have the right class to come back
331 Invoking C<Animal::speak> directly is a mess, however. What if
332 C<Animal::speak> didn't exist before, and was being inherited from a
333 class mentioned in C<@Animal::ISA>? Because we are no longer using
334 the method arrow, we get one and only one chance to hit the right
337 Also note that the C<Animal> classname is now hardwired into the
338 subroutine selection. This is a mess if someone maintains the code,
339 changing C<@ISA> for C<Mouse> and didn't notice C<Animal> there in
340 C<speak>. So, this is probably not the right way to go.
342 =head2 Starting the search from a different place
344 A better solution is to tell Perl to search from a higher place
345 in the inheritance chain:
347 # same Animal as before
349 # same @ISA, &sound as before
352 $class->Animal::speak;
353 print "[but you can barely hear it!]\n";
357 Ahh. This works. Using this syntax, we start with C<Animal> to find
358 C<speak>, and use all of C<Animal>'s inheritance chain if not found
359 immediately. And yet the first parameter will be C<$class>, so the
360 found C<speak> method will get C<Mouse> as its first entry, and
361 eventually work its way back to C<Mouse::sound> for the details.
363 But this isn't the best solution. We still have to keep the C<@ISA>
364 and the initial search package coordinated. Worse, if C<Mouse> had
365 multiple entries in C<@ISA>, we wouldn't necessarily know which one
366 had actually defined C<speak>. So, is there an even better way?
368 =head2 The SUPER way of doing things
370 By changing the C<Animal> class to the C<SUPER> class in that
371 invocation, we get a search of all of our super classes (classes
372 listed in C<@ISA>) automatically:
374 # same Animal as before
376 # same @ISA, &sound as before
379 $class->SUPER::speak;
380 print "[but you can barely hear it!]\n";
384 So, C<SUPER::speak> means look in the current package's C<@ISA> for
385 C<speak>, invoking the first one found. Note that it does I<not> look in
386 the C<@ISA> of C<$class>.
388 =head2 Where we're at so far...
390 So far, we've seen the method arrow syntax:
392 Class->method(@args);
399 which constructs an argument list of:
403 and attempts to invoke
405 Class::method("Class", @Args);
407 However, if C<Class::method> is not found, then C<@Class::ISA> is examined
408 (recursively) to locate a package that does indeed contain C<method>,
409 and that subroutine is invoked instead.
411 Using this simple syntax, we have class methods, (multiple)
412 inheritance, overriding, and extending. Using just what we've seen so
413 far, we've been able to factor out common code, and provide a nice way
414 to reuse implementations with variations. This is at the core of what
415 objects provide, but objects also provide instance data, which we
416 haven't even begun to cover.
418 =head2 A horse is a horse, of course of course -- or is it?
420 Let's start with the code for the C<Animal> class
421 and the C<Horse> class:
426 print "a $class goes ", $class->sound, "!\n"
431 sub sound { "neigh" }
434 This lets us invoke C<< Horse->speak >> to ripple upward to
435 C<Animal::speak>, calling back to C<Horse::sound> to get the specific
436 sound, and the output of:
440 But all of our Horse objects would have to be absolutely identical.
441 If I add a subroutine, all horses automatically share it. That's
442 great for making horses the same, but how do we capture the
443 distinctions about an individual horse? For example, suppose I want
444 to give my first horse a name. There's got to be a way to keep its
445 name separate from the other horses.
447 We can do that by drawing a new distinction, called an "instance".
448 An "instance" is generally created by a class. In Perl, any reference
449 can be an instance, so let's start with the simplest reference
450 that can hold a horse's name: a scalar reference.
453 my $talking = \$name;
455 So now C<$talking> is a reference to what will be the instance-specific
456 data (the name). The final step in turning this into a real instance
457 is with a special operator called C<bless>:
459 bless $talking, Horse;
461 This operator stores information about the package named C<Horse> into
462 the thing pointed at by the reference. At this point, we say
463 C<$talking> is an instance of C<Horse>. That is, it's a specific
464 horse. The reference is otherwise unchanged, and can still be used
465 with traditional dereferencing operators.
467 =head2 Invoking an instance method
469 The method arrow can be used on instances, as well as names of
470 packages (classes). So, let's get the sound that C<$talking> makes:
472 my $noise = $talking->sound;
474 To invoke C<sound>, Perl first notes that C<$talking> is a blessed
475 reference (and thus an instance). It then constructs an argument
476 list, in this case from just C<($talking)>. (Later we'll see that
477 arguments will take their place following the instance variable,
478 just like with classes.)
480 Now for the fun part: Perl takes the class in which the instance was
481 blessed, in this case C<Horse>, and uses that to locate the subroutine
482 to invoke the method. In this case, C<Horse::sound> is found directly
483 (without using inheritance), yielding the final subroutine invocation:
485 Horse::sound($talking)
487 Note that the first parameter here is still the instance, not the name
488 of the class as before. We'll get C<neigh> as the return value, and
489 that'll end up as the C<$noise> variable above.
491 If Horse::sound had not been found, we'd be wandering up the
492 C<@Horse::ISA> list to try to find the method in one of the
493 superclasses, just as for a class method. The only difference between
494 a class method and an instance method is whether the first parameter
495 is an instance (a blessed reference) or a class name (a string).
497 =head2 Accessing the instance data
499 Because we get the instance as the first parameter, we can now access
500 the instance-specific data. In this case, let's add a way to get at
505 sub sound { "neigh" }
512 Now we call for the name:
514 print $talking->name, " says ", $talking->sound, "\n";
516 Inside C<Horse::name>, the C<@_> array contains just C<$talking>,
517 which the C<shift> stores into C<$self>. (It's traditional to shift
518 the first parameter off into a variable named C<$self> for instance
519 methods, so stay with that unless you have strong reasons otherwise.)
520 Then, C<$self> gets de-referenced as a scalar ref, yielding C<Mr. Ed>,
521 and we're done with that. The result is:
525 =head2 How to build a horse
527 Of course, if we constructed all of our horses by hand, we'd most
528 likely make mistakes from time to time. We're also violating one of
529 the properties of object-oriented programming, in that the "inside
530 guts" of a Horse are visible. That's good if you're a veterinarian,
531 but not if you just like to own horses. So, let's let the Horse class
536 sub sound { "neigh" }
544 bless \$name, $class;
548 Now with the new C<named> method, we can build a horse:
550 my $talking = Horse->named("Mr. Ed");
552 Notice we're back to a class method, so the two arguments to
553 C<Horse::named> are C<Horse> and C<Mr. Ed>. The C<bless> operator
554 not only blesses C<$name>, it also returns the reference to C<$name>,
555 so that's fine as a return value. And that's how to build a horse.
557 We've called the constructor C<named> here, so that it quickly denotes
558 the constructor's argument as the name for this particular C<Horse>.
559 You can use different constructors with different names for different
560 ways of "giving birth" to the object (like maybe recording its
561 pedigree or date of birth). However, you'll find that most people
562 coming to Perl from more limited languages use a single constructor
563 named C<new>, with various ways of interpreting the arguments to
564 C<new>. Either style is fine, as long as you document your particular
565 way of giving birth to an object. (And you I<were> going to do that,
568 =head2 Inheriting the constructor
570 But was there anything specific to C<Horse> in that method? No. Therefore,
571 it's also the same recipe for building anything else that inherited from
572 C<Animal>, so let's put it there:
577 print "a $class goes ", $class->sound, "!\n"
586 bless \$name, $class;
591 sub sound { "neigh" }
594 Ahh, but what happens if we invoke C<speak> on an instance?
596 my $talking = Horse->named("Mr. Ed");
599 We get a debugging value:
601 a Horse=SCALAR(0xaca42ac) goes neigh!
603 Why? Because the C<Animal::speak> routine is expecting a classname as
604 its first parameter, not an instance. When the instance is passed in,
605 we'll end up using a blessed scalar reference as a string, and that
606 shows up as we saw it just now.
608 =head2 Making a method work with either classes or instances
610 All we need is for a method to detect if it is being called on a class
611 or called on an instance. The most straightforward way is with the
612 C<ref> operator. This returns a string (the classname) when used on a
613 blessed reference, and an empty string when used on a string (like a
614 classname). Let's modify the C<name> method first to notice the change:
619 ? $$either # it's an instance, return name
620 : "an unnamed $either"; # it's a class, return generic
623 Here, the C<?:> operator comes in handy to select either the
624 dereference or a derived string. Now we can use this with either an
625 instance or a class. Note that I've changed the first parameter
626 holder to C<$either> to show that this is intended:
628 my $talking = Horse->named("Mr. Ed");
629 print Horse->name, "\n"; # prints "an unnamed Horse\n"
630 print $talking->name, "\n"; # prints "Mr Ed.\n"
632 and now we'll fix C<speak> to use this:
636 print $either->name, " goes ", $either->sound, "\n";
639 And since C<sound> already worked with either a class or an instance,
642 =head2 Adding parameters to a method
644 Let's train our animals to eat:
650 bless \$name, $class;
655 ? $$either # it's an instance, return name
656 : "an unnamed $either"; # it's a class, return generic
660 print $either->name, " goes ", $either->sound, "\n";
665 print $either->name, " eats $food.\n";
670 sub sound { "neigh" }
674 sub sound { "baaaah" }
679 my $talking = Horse->named("Mr. Ed");
680 $talking->eat("hay");
686 an unnamed Sheep eats grass.
688 An instance method with parameters gets invoked with the instance,
689 and then the list of parameters. So that first invocation is like:
691 Animal::eat($talking, "hay");
693 =head2 More interesting instances
695 What if an instance needs more data? Most interesting instances are
696 made of many items, each of which can in turn be a reference or even
697 another object. The easiest way to store these is often in a hash.
698 The keys of the hash serve as the names of parts of the object (often
699 called "instance variables" or "member variables"), and the
700 corresponding values are, well, the values.
702 But how do we turn the horse into a hash? Recall that an object was
703 any blessed reference. We can just as easily make it a blessed hash
704 reference as a blessed scalar reference, as long as everything that
705 looks at the reference is changed accordingly.
707 Let's make a sheep that has a name and a color:
709 my $bad = bless { Name => "Evil", Color => "black" }, Sheep;
711 so C<< $bad->{Name} >> has C<Evil>, and C<< $bad->{Color} >> has
712 C<black>. But we want to make C<< $bad->name >> access the name, and
713 that's now messed up because it's expecting a scalar reference. Not
714 to worry, because that's pretty easy to fix up:
721 "an unnamed $either";
724 And of course C<named> still builds a scalar sheep, so let's fix that
731 my $self = { Name => $name, Color => $class->default_color };
735 What's this C<default_color>? Well, if C<named> has only the name,
736 we still need to set a color, so we'll have a class-specific initial color.
737 For a sheep, we might define it as white:
740 sub default_color { "white" }
742 And then to keep from having to define one for each additional class,
743 we'll define a "backstop" method that serves as the "default default",
744 directly in C<Animal>:
747 sub default_color { "brown" }
749 Now, because C<name> and C<named> were the only methods that
750 referenced the "structure" of the object, the rest of the methods can
751 remain the same, so C<speak> still works as before.
753 =head2 A horse of a different color
755 But having all our horses be brown would be boring. So let's add a
756 method or two to get and set the color.
763 $_[0]->{Color} = $_[1];
766 Note the alternate way of accessing the arguments: C<$_[0]> is used
767 in-place, rather than with a C<shift>. (This saves us a bit of time
768 for something that may be invoked frequently.) And now we can fix
769 that color for Mr. Ed:
771 my $talking = Horse->named("Mr. Ed");
772 $talking->set_color("black-and-white");
773 print $talking->name, " is colored ", $talking->color, "\n";
777 Mr. Ed is colored black-and-white
781 So, now we have class methods, constructors, instance methods,
782 instance data, and even accessors. But that's still just the
783 beginning of what Perl has to offer. We haven't even begun to talk
784 about accessors that double as getters and setters, destructors,
785 indirect object notation, subclasses that add instance data, per-class
786 data, overloading, "isa" and "can" tests, C<UNIVERSAL> class, and so
787 on. That's for the rest of the Perl documentation to cover.
788 Hopefully, this gets you started, though.
792 For more information, see L<perlobj> (for all the gritty details about
793 Perl objects, now that you've seen the basics), L<perltoot> (the
794 tutorial for those who already know objects), L<perltooc> (dealing
795 with class data), L<perlbot> (for some more tricks), and books such as
796 Damian Conway's excellent I<Object Oriented Perl>.
798 Some modules which might prove interesting are Class::Accessor,
799 Class::Class, Class::Contract, Class::Data::Inheritable,
800 Class::MethodMaker and Tie::SecureHash
804 Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge
805 Consulting Services, Inc. Permission is hereby granted to distribute
806 this document intact with the Perl distribution, and in accordance
807 with the licenses of the Perl distribution; derived documents must
808 include this copyright notice intact.
810 Portions of this text have been derived from Perl Training materials
811 originally appearing in the I<Packages, References, Objects, and
812 Modules> course taught by instructors for Stonehenge Consulting
813 Services, Inc. and used with permission.
815 Portions of this text have been derived from materials originally
816 appearing in I<Linux Magazine> and used with permission.