This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Small revisions to the text to increase clarity, suggested by Philip Monsen
[perl5.git] / pod / perlboot.pod
CommitLineData
694468e3
GS
1=head1 NAME
2
3perlboot - Beginner's Object-Oriented Tutorial
4
5=head1 DESCRIPTION
6
7If you're not familiar with objects from other languages, some of the
8other Perl object documentation may be a little daunting, such as
9L<perlobj>, a basic reference in using objects, and L<perltoot>, which
dbe48302 10introduces readers to the peculiarities of Perl's object system in a
694468e3
GS
11tutorial way.
12
13So, let's take a different approach, presuming no prior object
14experience. It helps if you know about subroutines (L<perlsub>),
15references (L<perlref> et. seq.), and packages (L<perlmod>), so become
16familiar with those first if you haven't already.
17
18=head2 If we could talk to the animals...
19
20Let's let the animals talk for a moment:
21
22 sub Cow::speak {
23 print "a Cow goes moooo!\n";
24 }
25 sub Horse::speak {
26 print "a Horse goes neigh!\n";
27 }
28 sub Sheep::speak {
bb32c4e1 29 print "a Sheep goes baaaah!\n";
694468e3
GS
30 }
31
32 Cow::speak;
33 Horse::speak;
34 Sheep::speak;
35
36This results in:
37
38 a Cow goes moooo!
39 a Horse goes neigh!
40 a Sheep goes baaaah!
41
42Nothing spectacular here. Simple subroutines, albeit from separate
43packages, and called using the full package name. So let's create
44an entire pasture:
45
46 # Cow::speak, Horse::speak, Sheep::speak as before
84f709e7
JH
47 @pasture = qw(Cow Cow Horse Sheep Sheep);
48 foreach $animal (@pasture) {
694468e3
GS
49 &{$animal."::speak"};
50 }
51
52This results in:
53
54 a Cow goes moooo!
55 a Cow goes moooo!
56 a Horse goes neigh!
57 a Sheep goes baaaah!
58 a Sheep goes baaaah!
59
60Wow. That symbolic coderef de-referencing there is pretty nasty.
074a7adc 61We're counting on C<no strict refs> mode, certainly not recommended
84f709e7
JH
62for larger programs. And why was that necessary? Because the name of
63the package seems to be inseparable from the name of the subroutine we
64want to invoke within that package.
694468e3
GS
65
66Or is it?
67
68=head2 Introducing the method invocation arrow
69
c47ff5f1 70For now, let's say that C<< Class->method >> invokes subroutine
694468e3
GS
71C<method> in package C<Class>. (Here, "Class" is used in its
72"category" meaning, not its "scholastic" meaning.) That's not
73completely accurate, but we'll do this one step at a time. Now let's
74use it like so:
75
76 # Cow::speak, Horse::speak, Sheep::speak as before
77 Cow->speak;
78 Horse->speak;
79 Sheep->speak;
80
81And once again, this results in:
82
83 a Cow goes moooo!
84 a Horse goes neigh!
85 a Sheep goes baaaah!
86
87That's not fun yet. Same number of characters, all constant, no
88variables. But yet, the parts are separable now. Watch:
89
84f709e7 90 $a = "Cow";
694468e3
GS
91 $a->speak; # invokes Cow->speak
92
93Ahh! Now that the package name has been parted from the subroutine
94name, we can use a variable package name. And this time, we've got
84f709e7 95something that works even when C<use strict refs> is enabled.
694468e3
GS
96
97=head2 Invoking a barnyard
98
99Let's take that new arrow invocation and put it back in the barnyard
100example:
101
102 sub Cow::speak {
103 print "a Cow goes moooo!\n";
104 }
105 sub Horse::speak {
106 print "a Horse goes neigh!\n";
107 }
108 sub Sheep::speak {
bb32c4e1 109 print "a Sheep goes baaaah!\n";
694468e3
GS
110 }
111
84f709e7
JH
112 @pasture = qw(Cow Cow Horse Sheep Sheep);
113 foreach $animal (@pasture) {
694468e3
GS
114 $animal->speak;
115 }
116
117There! Now we have the animals all talking, and safely at that,
118without the use of symbolic coderefs.
119
120But look at all that common code. Each of the C<speak> routines has a
121similar structure: a C<print> operator and a string that contains
122common text, except for two of the words. It'd be nice if we could
123factor out the commonality, in case we decide later to change it all
124to C<says> instead of C<goes>.
125
126And we actually have a way of doing that without much fuss, but we
127have to hear a bit more about what the method invocation arrow is
128actually doing for us.
129
130=head2 The extra parameter of method invocation
131
132The invocation of:
133
134 Class->method(@args)
135
136attempts to invoke subroutine C<Class::method> as:
137
138 Class::method("Class", @args);
139
140(If the subroutine can't be found, "inheritance" kicks in, but we'll
141get to that later.) This means that we get the class name as the
dbe48302
GS
142first parameter (the only parameter, if no arguments are given). So
143we can rewrite the C<Sheep> speaking subroutine as:
694468e3
GS
144
145 sub Sheep::speak {
146 my $class = shift;
147 print "a $class goes baaaah!\n";
148 }
149
150And the other two animals come out similarly:
151
152 sub Cow::speak {
153 my $class = shift;
154 print "a $class goes moooo!\n";
155 }
156 sub Horse::speak {
157 my $class = shift;
158 print "a $class goes neigh!\n";
159 }
160
161In each case, C<$class> will get the value appropriate for that
162subroutine. But once again, we have a lot of similar structure. Can
163we factor that out even further? Yes, by calling another method in
164the same class.
165
166=head2 Calling a second method to simplify things
167
168Let's call out from C<speak> to a helper method called C<sound>.
169This method provides the constant text for the sound itself.
170
84f709e7 171 { package Cow;
694468e3
GS
172 sub sound { "moooo" }
173 sub speak {
549d696a
MW
174 my $class = shift;
175 print "a $class goes ", $class->sound, "!\n";
694468e3
GS
176 }
177 }
178
c47ff5f1
GS
179Now, when we call C<< Cow->speak >>, we get a C<$class> of C<Cow> in
180C<speak>. This in turn selects the C<< Cow->sound >> method, which
694468e3
GS
181returns C<moooo>. But how different would this be for the C<Horse>?
182
84f709e7 183 { package Horse;
694468e3
GS
184 sub sound { "neigh" }
185 sub speak {
549d696a
MW
186 my $class = shift;
187 print "a $class goes ", $class->sound, "!\n";
694468e3
GS
188 }
189 }
190
191Only the name of the package and the specific sound change. So can we
192somehow share the definition for C<speak> between the Cow and the
193Horse? Yes, with inheritance!
194
195=head2 Inheriting the windpipes
196
197We'll define a common subroutine package called C<Animal>, with the
198definition for C<speak>:
199
84f709e7 200 { package Animal;
694468e3 201 sub speak {
549d696a
MW
202 my $class = shift;
203 print "a $class goes ", $class->sound, "!\n";
694468e3
GS
204 }
205 }
206
207Then, for each animal, we say it "inherits" from C<Animal>, along
208with the animal-specific sound:
209
84f709e7 210 { package Cow;
694468e3
GS
211 @ISA = qw(Animal);
212 sub sound { "moooo" }
213 }
214
4dfdcbc9 215Note the added C<@ISA> array (pronounced "is a"). We'll get to that in a minute.
694468e3 216
c47ff5f1 217But what happens when we invoke C<< Cow->speak >> now?
694468e3
GS
218
219First, Perl constructs the argument list. In this case, it's just
220C<Cow>. Then Perl looks for C<Cow::speak>. But that's not there, so
221Perl checks for the inheritance array C<@Cow::ISA>. It's there,
222and contains the single name C<Animal>.
223
224Perl next checks for C<speak> inside C<Animal> instead, as in
225C<Animal::speak>. And that's found, so Perl invokes that subroutine
226with the already frozen argument list.
227
228Inside the C<Animal::speak> subroutine, C<$class> becomes C<Cow> (the
229first argument). So when we get to the step of invoking
c47ff5f1 230C<< $class->sound >>, it'll be looking for C<< Cow->sound >>, which
694468e3
GS
231gets it on the first try without looking at C<@ISA>. Success!
232
233=head2 A few notes about @ISA
234
4dfdcbc9
MW
235This magical C<@ISA> variable has declared that C<Cow> "is a" C<Animal>.
236Note that it's an array, not a simple single value, because on rare
237occasions, it makes sense to have more than one parent class searched
238for the missing methods.
694468e3
GS
239
240If C<Animal> also had an C<@ISA>, then we'd check there too. The
dd69841b
BB
241search is recursive, depth-first, left-to-right in each C<@ISA> by
242default (see L<mro> for alternatives). Typically, each C<@ISA> has
243only one element (multiple elements means multiple inheritance and
244multiple headaches), so we get a nice tree of inheritance.
694468e3
GS
245
246When we turn on C<use strict>, we'll get complaints on C<@ISA>, since
247it's not a variable containing an explicit package name, nor is it a
dbe48302
GS
248lexical ("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),
694468e3
GS
250so there's a couple of straightforward ways to handle that.
251
252The easiest is to just spell the package name out:
253
254 @Cow::ISA = qw(Animal);
255
dbe1c45d 256Or declare it as a package global variable:
016e9c56
RGS
257
258 package Cow;
259 our @ISA = qw(Animal);
260
dbe48302 261Or allow it as an implicitly named package variable:
694468e3
GS
262
263 package Cow;
84f709e7
JH
264 use vars qw(@ISA);
265 @ISA = qw(Animal);
694468e3 266
2ee409f9
MW
267If the C<Animal> class comes from another (object-oriented) module, then
268just employ C<use base> to specify that C<Animal> should serve as the basis
269for the C<Cow> class:
694468e3
GS
270
271 package Cow;
272 use base qw(Animal);
273
2ee409f9 274Now that's pretty darn simple!
694468e3
GS
275
276=head2 Overriding the methods
277
278Let's add a mouse, which can barely be heard:
279
84f709e7
JH
280 # Animal package from before
281 { package Mouse;
282 @ISA = qw(Animal);
694468e3
GS
283 sub sound { "squeak" }
284 sub speak {
285 my $class = shift;
549d696a
MW
286 print "a $class goes ", $class->sound, "!\n";
287 print "[but you can barely hear it!]\n";
694468e3
GS
288 }
289 }
290
291 Mouse->speak;
292
293which results in:
294
295 a Mouse goes squeak!
296 [but you can barely hear it!]
297
c47ff5f1 298Here, C<Mouse> has its own speaking routine, so C<< Mouse->speak >>
c98e0e85
MW
299doesn't immediately invoke C<< Animal->speak >>. This is known as
300"overriding". In fact, we don't even need to say that a C<Mouse> is
301an C<Animal> at all, because all of the methods needed for C<speak> are
302completely defined for C<Mouse>; this is known as "duck typing":
303"If it walks like a duck and quacks like a duck, I would call it a duck"
304(James Whitcomb). However, it would probably be beneficial to allow a
305closer examination to conclude that a C<Mouse> is indeed an C<Animal>,
306so it is actually better to define C<Mouse> with C<Animal> as its base
307(that is, it is better to "derive C<Mouse> from C<Animal>").
308
309Moreover, this duplication of code could become a maintenance headache
310(though code-reuse is not actually a good reason for inheritance; good
311design practices dictate that a derived class should be usable wherever
312its base class is usable, which might not be the outcome if code-reuse
313is the sole criterion for inheritance. Just remember that a C<Mouse>
314should always act like an C<Animal>).
315
316So, let's make C<Mouse> an C<Animal>!
694468e3 317
c7b05296 318The obvious solution is to invoke C<Animal::speak> directly:
694468e3 319
84f709e7
JH
320 # Animal package from before
321 { package Mouse;
322 @ISA = qw(Animal);
694468e3
GS
323 sub sound { "squeak" }
324 sub speak {
325 my $class = shift;
326 Animal::speak($class);
549d696a 327 print "[but you can barely hear it!]\n";
694468e3
GS
328 }
329 }
330
c7b05296
MW
331Note that we're using C<Animal::speak>. If we were to invoke
332C<< Animal->speak >> instead, the first parameter to C<Animal::speak>
333would automatically be C<"Animal"> rather than C<"Mouse">, so that
334the call to C<< $class->sound >> in C<Animal::speak> would become
335C<< Animal->sound >> rather than C<< Mouse->sound >>.
336
337Also, without the method arrow C<< -> >>, it becomes necessary to specify
338the first parameter to C<Animal::speak> ourselves, which is why C<$class>
339is explicitly passed: C<Animal::speak($class)>.
340
341However, invoking C<Animal::speak> directly is a mess: Firstly, it assumes
342that the C<speak> method is a member of the C<Animal> class; what if C<Animal>
343actually inherits C<speak> from its own base? Because we are no longer using
344C<< -> >> to access C<speak>, the special method look up mechanism wouldn't be
345used, so C<speak> wouldn't even be found!
346
347The second problem is more subtle: C<Animal> is now hardwired into the subroutine
348selection. Let's assume that C<Animal::speak> does exist. What happens when,
349at a later time, someone expands the class hierarchy by having C<Mouse>
350inherit from C<Mus> instead of C<Animal>. Unless the invocation of C<Animal::speak>
351is also changed to an invocation of C<Mus::speak>, centuries worth of taxonomical
352classification could be obliterated!
353
354What we have here is a fragile or leaky abstraction; it is the beginning of a
355maintenance nightmare. What we need is the ability to search for the right
356method wih as few assumptions as possible.
694468e3
GS
357
358=head2 Starting the search from a different place
359
c7b05296
MW
360A I<better> solution is to tell Perl where in the inheritance chain to begin searching
361for C<speak>. This can be achieved with a modified version of the method arrow C<< -> >>:
362
363 ClassName->FirstPlaceToLook::method
364
365So, the improved C<Mouse> class is:
694468e3
GS
366
367 # same Animal as before
84f709e7 368 { package Mouse;
694468e3
GS
369 # same @ISA, &sound as before
370 sub speak {
371 my $class = shift;
372 $class->Animal::speak;
373 print "[but you can barely hear it!]\n";
374 }
375 }
376
c7b05296
MW
377Using this syntax, we start with C<Animal> to find C<speak>, and then
378use all of C<Animal>'s inheritance chain if it is not found immediately.
379As usual, the first parameter to C<speak> would be C<$class>, so we no
380longer need to pass C<$class> explicitly to C<speak>.
694468e3 381
c7b05296
MW
382But what about the second problem? We're still hardwiring C<Animal> into
383the method lookup.
694468e3
GS
384
385=head2 The SUPER way of doing things
386
c7b05296
MW
387If C<Animal> is replaced with the special placeholder C<SUPER> in that
388invocation, then the contents of C<Mouse>'s C<@ISA> are used for the
389search, beginning with C<$ISA[0]>. So, all of the problems can be fixed
390as follows:
694468e3
GS
391
392 # same Animal as before
84f709e7 393 { package Mouse;
694468e3
GS
394 # same @ISA, &sound as before
395 sub speak {
396 my $class = shift;
397 $class->SUPER::speak;
398 print "[but you can barely hear it!]\n";
399 }
400 }
401
c7b05296
MW
402In general, C<SUPER::speak> means look in the current package's C<@ISA>
403for a class that implements C<speak>, and invoke the first one found.
404The placeholder is called C<SUPER>, because many other languages refer
405to base classes as "I<super>classes", and Perl likes to be eclectic.
406
407Note that a call such as
408
409 $class->SUPER::method;
410
411does I<not> look in the C<@ISA> of C<$class> unless C<$class> happens to
412be the current package.
694468e3 413
507fa6a5 414=head2 Let's review...
694468e3
GS
415
416So far, we've seen the method arrow syntax:
417
418 Class->method(@args);
419
420or the equivalent:
421
84f709e7 422 $a = "Class";
694468e3
GS
423 $a->method(@args);
424
425which constructs an argument list of:
426
427 ("Class", @args)
428
32914eef 429and attempts to invoke:
694468e3 430
507fa6a5 431 Class::method("Class", @args);
694468e3
GS
432
433However, if C<Class::method> is not found, then C<@Class::ISA> is examined
32914eef 434(recursively) to locate a class (a package) that does indeed contain C<method>,
694468e3
GS
435and that subroutine is invoked instead.
436
507fa6a5
MW
437Using this simple syntax, we have class methods, (multiple) inheritance,
438overriding, and extending. Using just what we've seen so far, we've
439been able to factor out common code (though that's never a good reason
440for inheritance!), and provide a nice way to reuse implementations with
441variations.
442
443Now, what about data?
694468e3 444
ac036724 445=head2 A horse is a horse, of course of course, or is it?
694468e3
GS
446
447Let's start with the code for the C<Animal> class
448and the C<Horse> class:
449
84f709e7 450 { package Animal;
694468e3
GS
451 sub speak {
452 my $class = shift;
bb32c4e1 453 print "a $class goes ", $class->sound, "!\n";
694468e3
GS
454 }
455 }
84f709e7
JH
456 { package Horse;
457 @ISA = qw(Animal);
694468e3
GS
458 sub sound { "neigh" }
459 }
460
c47ff5f1 461This lets us invoke C<< Horse->speak >> to ripple upward to
694468e3
GS
462C<Animal::speak>, calling back to C<Horse::sound> to get the specific
463sound, and the output of:
464
465 a Horse goes neigh!
466
467But all of our Horse objects would have to be absolutely identical.
32914eef 468If we add a subroutine, all horses automatically share it. That's
694468e3 469great for making horses the same, but how do we capture the
32914eef
MW
470distinctions of an individual horse? For example, suppose we want
471to give our first horse a name. There's got to be a way to keep its
694468e3
GS
472name separate from the other horses.
473
32914eef
MW
474That is to say, we want particular instances of C<Horse> to have
475different names.
476
477In Perl, any reference can be an "instance", so let's start with the
478simplest reference that can hold a horse's name: a scalar reference.
694468e3 479
84f709e7 480 my $name = "Mr. Ed";
32914eef 481 my $horse = \$name;
694468e3 482
32914eef
MW
483So, now C<$horse> is a reference to what will be the instance-specific
484data (the name). The final step is to turn this reference into a real
485instance of a C<Horse> by using the special operator C<bless>:
694468e3 486
32914eef 487 bless $horse, Horse;
694468e3
GS
488
489This operator stores information about the package named C<Horse> into
490the thing pointed at by the reference. At this point, we say
32914eef 491C<$horse> is an instance of C<Horse>. That is, it's a specific
694468e3
GS
492horse. The reference is otherwise unchanged, and can still be used
493with traditional dereferencing operators.
494
495=head2 Invoking an instance method
496
32914eef
MW
497The method arrow can be used on instances, as well as classes (the names
498of packages). So, let's get the sound that C<$horse> makes:
694468e3 499
32914eef 500 my $noise = $horse->sound("some", "unnecessary", "args");
694468e3 501
32914eef 502To invoke C<sound>, Perl first notes that C<$horse> is a blessed
694468e3 503reference (and thus an instance). It then constructs an argument
32914eef 504list, as per usual.
694468e3
GS
505
506Now for the fun part: Perl takes the class in which the instance was
9fd5bac0 507blessed, in this case C<Horse>, and uses that class to locate the
32914eef
MW
508subroutine. In this case, C<Horse::sound> is found directly (without
509using inheritance). In the end, it is as though our initial line were
510written as follows:
694468e3 511
32914eef 512 my $noise = Horse::sound($horse, "some", "unnecessary", "args");
694468e3
GS
513
514Note that the first parameter here is still the instance, not the name
515of the class as before. We'll get C<neigh> as the return value, and
516that'll end up as the C<$noise> variable above.
517
32914eef
MW
518If Horse::sound had not been found, we'd be wandering up the C<@Horse::ISA>
519array, trying to find the method in one of the superclasses. The only
520difference between a class method and an instance method is whether the
521first parameter is an instance (a blessed reference) or a class name (a
522string).
694468e3
GS
523
524=head2 Accessing the instance data
525
526Because we get the instance as the first parameter, we can now access
527the instance-specific data. In this case, let's add a way to get at
528the name:
529
84f709e7
JH
530 { package Horse;
531 @ISA = qw(Animal);
694468e3
GS
532 sub sound { "neigh" }
533 sub name {
534 my $self = shift;
535 $$self;
536 }
537 }
c47ff5f1 538
32914eef
MW
539Inside C<Horse::name>, the C<@_> array contains:
540
ea8b8ad2 541 ($horse, "some", "unnecessary", "args")
32914eef
MW
542
543so the C<shift> stores C<$horse> into C<$self>. Then, C<$self> gets
544de-referenced with C<$$self> as normal, yielding C<"Mr. Ed">.
545
546It's traditional to C<shift> the first parameter into a variable named
547C<$self> for instance methods and into a variable named C<$class> for
548class methods.
549
550Then, the following line:
694468e3 551
32914eef 552 print $horse->name, " says ", $horse->sound, "\n";
694468e3 553
32914eef 554outputs:
694468e3
GS
555
556 Mr. Ed says neigh.
557
558=head2 How to build a horse
559
560Of course, if we constructed all of our horses by hand, we'd most
561likely make mistakes from time to time. We're also violating one of
562the properties of object-oriented programming, in that the "inside
563guts" of a Horse are visible. That's good if you're a veterinarian,
64261f91
MW
564but not if you just like to own horses. So, let's have the Horse
565class handle the details inside a class method:
694468e3 566
84f709e7
JH
567 { package Horse;
568 @ISA = qw(Animal);
694468e3
GS
569 sub sound { "neigh" }
570 sub name {
33739d6a 571 my $self = shift; # instance method, so use $self
694468e3
GS
572 $$self;
573 }
574 sub named {
33739d6a 575 my $class = shift; # class method, so use $class
84f709e7 576 my $name = shift;
694468e3
GS
577 bless \$name, $class;
578 }
579 }
580
64261f91 581Now with the new C<named> method, we can build a horse as follows:
694468e3 582
32914eef 583 my $horse = Horse->named("Mr. Ed");
694468e3
GS
584
585Notice we're back to a class method, so the two arguments to
586C<Horse::named> are C<Horse> and C<Mr. Ed>. The C<bless> operator
9fd5bac0 587not only blesses C<\$name>, it also returns that reference.
64261f91
MW
588
589This C<Horse::named> method is called a "constructor".
694468e3 590
dbe48302
GS
591We've called the constructor C<named> here, so that it quickly denotes
592the constructor's argument as the name for this particular C<Horse>.
593You can use different constructors with different names for different
594ways of "giving birth" to the object (like maybe recording its
595pedigree or date of birth). However, you'll find that most people
596coming to Perl from more limited languages use a single constructor
597named C<new>, with various ways of interpreting the arguments to
598C<new>. Either style is fine, as long as you document your particular
599way of giving birth to an object. (And you I<were> going to do that,
600right?)
601
694468e3
GS
602=head2 Inheriting the constructor
603
604But was there anything specific to C<Horse> in that method? No. Therefore,
605it's also the same recipe for building anything else that inherited from
c30f1015 606C<Animal>, so let's put C<name> and C<named> there:
694468e3 607
84f709e7 608 { package Animal;
694468e3
GS
609 sub speak {
610 my $class = shift;
bb32c4e1 611 print "a $class goes ", $class->sound, "!\n";
694468e3
GS
612 }
613 sub name {
614 my $self = shift;
615 $$self;
616 }
617 sub named {
618 my $class = shift;
84f709e7 619 my $name = shift;
694468e3
GS
620 bless \$name, $class;
621 }
622 }
84f709e7
JH
623 { package Horse;
624 @ISA = qw(Animal);
694468e3
GS
625 sub sound { "neigh" }
626 }
627
628Ahh, but what happens if we invoke C<speak> on an instance?
629
32914eef
MW
630 my $horse = Horse->named("Mr. Ed");
631 $horse->speak;
694468e3
GS
632
633We get a debugging value:
634
635 a Horse=SCALAR(0xaca42ac) goes neigh!
636
637Why? Because the C<Animal::speak> routine is expecting a classname as
638its first parameter, not an instance. When the instance is passed in,
639we'll end up using a blessed scalar reference as a string, and that
640shows up as we saw it just now.
641
642=head2 Making a method work with either classes or instances
643
644All we need is for a method to detect if it is being called on a class
645or called on an instance. The most straightforward way is with the
646C<ref> operator. This returns a string (the classname) when used on a
3e9e48b0 647blessed reference, and an empty string when used on a string (like a
694468e3
GS
648classname). Let's modify the C<name> method first to notice the change:
649
650 sub name {
651 my $either = shift;
c30f1015 652 ref $either ? $$either : "Any $either";
694468e3
GS
653 }
654
655Here, the C<?:> operator comes in handy to select either the
656dereference or a derived string. Now we can use this with either an
657instance or a class. Note that I've changed the first parameter
658holder to C<$either> to show that this is intended:
659
32914eef 660 my $horse = Horse->named("Mr. Ed");
c30f1015 661 print Horse->name, "\n"; # prints "Any Horse\n"
32914eef 662 print $horse->name, "\n"; # prints "Mr Ed.\n"
694468e3
GS
663
664and now we'll fix C<speak> to use this:
665
666 sub speak {
667 my $either = shift;
668 print $either->name, " goes ", $either->sound, "\n";
669 }
670
671And since C<sound> already worked with either a class or an instance,
672we're done!
673
674=head2 Adding parameters to a method
675
676Let's train our animals to eat:
677
84f709e7 678 { package Animal;
694468e3
GS
679 sub named {
680 my $class = shift;
84f709e7 681 my $name = shift;
694468e3
GS
682 bless \$name, $class;
683 }
684 sub name {
685 my $either = shift;
c30f1015 686 ref $either ? $$either : "Any $either";
694468e3
GS
687 }
688 sub speak {
689 my $either = shift;
690 print $either->name, " goes ", $either->sound, "\n";
691 }
692 sub eat {
693 my $either = shift;
84f709e7 694 my $food = shift;
694468e3
GS
695 print $either->name, " eats $food.\n";
696 }
697 }
84f709e7
JH
698 { package Horse;
699 @ISA = qw(Animal);
694468e3
GS
700 sub sound { "neigh" }
701 }
84f709e7
JH
702 { package Sheep;
703 @ISA = qw(Animal);
694468e3
GS
704 sub sound { "baaaah" }
705 }
706
707And now try it out:
708
32914eef
MW
709 my $horse = Horse->named("Mr. Ed");
710 $horse->eat("hay");
694468e3
GS
711 Sheep->eat("grass");
712
713which prints:
714
715 Mr. Ed eats hay.
c30f1015 716 Any Sheep eats grass.
694468e3
GS
717
718An instance method with parameters gets invoked with the instance,
719and then the list of parameters. So that first invocation is like:
720
32914eef 721 Animal::eat($horse, "hay");
694468e3
GS
722
723=head2 More interesting instances
724
725What if an instance needs more data? Most interesting instances are
726made of many items, each of which can in turn be a reference or even
727another object. The easiest way to store these is often in a hash.
728The keys of the hash serve as the names of parts of the object (often
729called "instance variables" or "member variables"), and the
730corresponding values are, well, the values.
731
732But how do we turn the horse into a hash? Recall that an object was
733any blessed reference. We can just as easily make it a blessed hash
734reference as a blessed scalar reference, as long as everything that
735looks at the reference is changed accordingly.
736
737Let's make a sheep that has a name and a color:
738
84f709e7 739 my $bad = bless { Name => "Evil", Color => "black" }, Sheep;
694468e3 740
c47ff5f1
GS
741so C<< $bad->{Name} >> has C<Evil>, and C<< $bad->{Color} >> has
742C<black>. But we want to make C<< $bad->name >> access the name, and
694468e3 743that's now messed up because it's expecting a scalar reference. Not
9cbc8da9
MW
744to worry, because that's pretty easy to fix up.
745
746One solution is to override C<Animal::name> and C<Animal::named> by
747defining them anew in C<Sheep>, but then any methods added later to
748C<Animal> might still mess up, and we'd have to override all of those
749too. Therefore, it's never a good idea to define the data layout in a
750way that's different from the data layout of the base classes. In fact,
751it's a good idea to use blessed hash references in all cases. Also, this
9fd5bac0
MW
752is why it's important to have constructors do the low-level work. So,
753let's redefine C<Animal>:
694468e3
GS
754
755 ## in Animal
756 sub name {
757 my $either = shift;
c30f1015 758 ref $either ? $either->{Name} : "Any $either";
694468e3 759 }
694468e3
GS
760 sub named {
761 my $class = shift;
84f709e7 762 my $name = shift;
9cbc8da9 763 my $self = { Name => $name };
694468e3
GS
764 bless $self, $class;
765 }
766
9cbc8da9
MW
767Of course, we still need to override C<named> in order to handle
768constructing a C<Sheep> with a certain color:
769
770 ## in Sheep
771 sub named {
772 my ($class, $name) = @_;
773 my $self = $class->SUPER::named(@_);
774 $$self{Color} = $class->default_color;
775 $self
776 }
777
778(Note that C<@_> contains the parameters to C<named>.)
779
694468e3 780What's this C<default_color>? Well, if C<named> has only the name,
9cbc8da9 781we still need to set a color, so we'll have a class-specific default color.
694468e3
GS
782For a sheep, we might define it as white:
783
784 ## in Sheep
785 sub default_color { "white" }
786
9cbc8da9
MW
787Now:
788
789 my $sheep = Sheep->named("Bad");
790 print $sheep->{Color}, "\n";
791
792outputs:
793
794 white
795
796Now, there's nothing particularly specific to C<Sheep> when it comes
797to color, so let's remove C<Sheep::named> and implement C<Animal::named>
798to handle color instead:
799
800 ## in Animal
801 sub named {
802 my ($class, $name) = @_;
803 my $self = { Name => $name, Color => $class->default_color };
804 bless $self, $class;
805 }
806
807And then to keep from having to define C<default_color> for each additional
808class, we'll define a method that serves as the "default default" directly
809in C<Animal>:
694468e3
GS
810
811 ## in Animal
812 sub default_color { "brown" }
813
9cbc8da9 814Of course, because C<name> and C<named> were the only methods that
694468e3
GS
815referenced the "structure" of the object, the rest of the methods can
816remain the same, so C<speak> still works as before.
817
818=head2 A horse of a different color
819
820But having all our horses be brown would be boring. So let's add a
821method or two to get and set the color.
822
823 ## in Animal
824 sub color {
825 $_[0]->{Color}
826 }
827 sub set_color {
828 $_[0]->{Color} = $_[1];
829 }
830
831Note the alternate way of accessing the arguments: C<$_[0]> is used
832in-place, rather than with a C<shift>. (This saves us a bit of time
833for something that may be invoked frequently.) And now we can fix
834that color for Mr. Ed:
835
32914eef
MW
836 my $horse = Horse->named("Mr. Ed");
837 $horse->set_color("black-and-white");
838 print $horse->name, " is colored ", $horse->color, "\n";
694468e3
GS
839
840which results in:
841
842 Mr. Ed is colored black-and-white
843
844=head2 Summary
845
9014bf7f
MW
846So, now we have class methods, constructors, instance methods, instance
847data, and even accessors. But that's still just the beginning of what
848Perl has to offer. We haven't even begun to talk about accessors that
849double as getters and setters, destructors, indirect object notation,
850overloading, "isa" and "can" tests, the C<UNIVERSAL> class, and so on.
851That's for the rest of the Perl documentation to cover. Hopefully, this
852gets you started, though.
694468e3
GS
853
854=head1 SEE ALSO
855
856For more information, see L<perlobj> (for all the gritty details about
857Perl objects, now that you've seen the basics), L<perltoot> (the
890a53b9 858tutorial for those who already know objects), L<perltooc> (dealing
8257a158
MS
859with class data), L<perlbot> (for some more tricks), and books such as
860Damian Conway's excellent I<Object Oriented Perl>.
861
862Some modules which might prove interesting are Class::Accessor,
863Class::Class, Class::Contract, Class::Data::Inheritable,
864Class::MethodMaker and Tie::SecureHash
694468e3
GS
865
866=head1 COPYRIGHT
867
868Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge
9014bf7f
MW
869Consulting Services, Inc.
870
871Copyright (c) 2009 by Michael F. Witten.
872
873Permission is hereby granted to distribute this document intact with
874the Perl distribution, and in accordance with the licenses of the Perl
875distribution; derived documents must include this copyright notice
876intact.
694468e3
GS
877
878Portions of this text have been derived from Perl Training materials
879originally appearing in the I<Packages, References, Objects, and
880Modules> course taught by instructors for Stonehenge Consulting
881Services, Inc. and used with permission.
882
883Portions of this text have been derived from materials originally
884appearing in I<Linux Magazine> and used with permission.