Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLBOOT 1" | |
132 | .TH PERLBOOT 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlboot \- Beginner's Object\-Oriented Tutorial | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | If you're not familiar with objects from other languages, some of the | |
138 | other Perl object documentation may be a little daunting, such as | |
139 | perlobj, a basic reference in using objects, and perltoot, which | |
140 | introduces readers to the peculiarities of Perl's object system in a | |
141 | tutorial way. | |
142 | .PP | |
143 | So, let's take a different approach, presuming no prior object | |
144 | experience. It helps if you know about subroutines (perlsub), | |
145 | references (perlref et. seq.), and packages (perlmod), so become | |
146 | familiar with those first if you haven't already. | |
147 | .Sh "If we could talk to the animals..." | |
148 | .IX Subsection "If we could talk to the animals..." | |
149 | Let's let the animals talk for a moment: | |
150 | .PP | |
151 | .Vb 9 | |
152 | \& sub Cow::speak { | |
153 | \& print "a Cow goes moooo!\en"; | |
154 | \& } | |
155 | \& sub Horse::speak { | |
156 | \& print "a Horse goes neigh!\en"; | |
157 | \& } | |
158 | \& sub Sheep::speak { | |
159 | \& print "a Sheep goes baaaah!\en" | |
160 | \& } | |
161 | .Ve | |
162 | .PP | |
163 | .Vb 3 | |
164 | \& Cow::speak; | |
165 | \& Horse::speak; | |
166 | \& Sheep::speak; | |
167 | .Ve | |
168 | .PP | |
169 | This results in: | |
170 | .PP | |
171 | .Vb 3 | |
172 | \& a Cow goes moooo! | |
173 | \& a Horse goes neigh! | |
174 | \& a Sheep goes baaaah! | |
175 | .Ve | |
176 | .PP | |
177 | Nothing spectacular here. Simple subroutines, albeit from separate | |
178 | packages, and called using the full package name. So let's create | |
179 | an entire pasture: | |
180 | .PP | |
181 | .Vb 5 | |
182 | \& # Cow::speak, Horse::speak, Sheep::speak as before | |
183 | \& @pasture = qw(Cow Cow Horse Sheep Sheep); | |
184 | \& foreach $animal (@pasture) { | |
185 | \& &{$animal."::speak"}; | |
186 | \& } | |
187 | .Ve | |
188 | .PP | |
189 | This results in: | |
190 | .PP | |
191 | .Vb 5 | |
192 | \& a Cow goes moooo! | |
193 | \& a Cow goes moooo! | |
194 | \& a Horse goes neigh! | |
195 | \& a Sheep goes baaaah! | |
196 | \& a Sheep goes baaaah! | |
197 | .Ve | |
198 | .PP | |
199 | Wow. That symbolic coderef de-referencing there is pretty nasty. | |
200 | We're counting on \f(CW\*(C`no strict subs\*(C'\fR mode, certainly not recommended | |
201 | for larger programs. And why was that necessary? Because the name of | |
202 | the package seems to be inseparable from the name of the subroutine we | |
203 | want to invoke within that package. | |
204 | .PP | |
205 | Or is it? | |
206 | .Sh "Introducing the method invocation arrow" | |
207 | .IX Subsection "Introducing the method invocation arrow" | |
208 | For now, let's say that \f(CW\*(C`Class\->method\*(C'\fR invokes subroutine | |
209 | \&\f(CW\*(C`method\*(C'\fR in package \f(CW\*(C`Class\*(C'\fR. (Here, \*(L"Class\*(R" is used in its | |
210 | \&\*(L"category\*(R" meaning, not its \*(L"scholastic\*(R" meaning.) That's not | |
211 | completely accurate, but we'll do this one step at a time. Now let's | |
212 | use it like so: | |
213 | .PP | |
214 | .Vb 4 | |
215 | \& # Cow::speak, Horse::speak, Sheep::speak as before | |
216 | \& Cow->speak; | |
217 | \& Horse->speak; | |
218 | \& Sheep->speak; | |
219 | .Ve | |
220 | .PP | |
221 | And once again, this results in: | |
222 | .PP | |
223 | .Vb 3 | |
224 | \& a Cow goes moooo! | |
225 | \& a Horse goes neigh! | |
226 | \& a Sheep goes baaaah! | |
227 | .Ve | |
228 | .PP | |
229 | That's not fun yet. Same number of characters, all constant, no | |
230 | variables. But yet, the parts are separable now. Watch: | |
231 | .PP | |
232 | .Vb 2 | |
233 | \& $a = "Cow"; | |
234 | \& $a->speak; # invokes Cow->speak | |
235 | .Ve | |
236 | .PP | |
237 | Ahh! Now that the package name has been parted from the subroutine | |
238 | name, we can use a variable package name. And this time, we've got | |
239 | something that works even when \f(CW\*(C`use strict refs\*(C'\fR is enabled. | |
240 | .Sh "Invoking a barnyard" | |
241 | .IX Subsection "Invoking a barnyard" | |
242 | Let's take that new arrow invocation and put it back in the barnyard | |
243 | example: | |
244 | .PP | |
245 | .Vb 9 | |
246 | \& sub Cow::speak { | |
247 | \& print "a Cow goes moooo!\en"; | |
248 | \& } | |
249 | \& sub Horse::speak { | |
250 | \& print "a Horse goes neigh!\en"; | |
251 | \& } | |
252 | \& sub Sheep::speak { | |
253 | \& print "a Sheep goes baaaah!\en" | |
254 | \& } | |
255 | .Ve | |
256 | .PP | |
257 | .Vb 4 | |
258 | \& @pasture = qw(Cow Cow Horse Sheep Sheep); | |
259 | \& foreach $animal (@pasture) { | |
260 | \& $animal->speak; | |
261 | \& } | |
262 | .Ve | |
263 | .PP | |
264 | There! Now we have the animals all talking, and safely at that, | |
265 | without the use of symbolic coderefs. | |
266 | .PP | |
267 | But look at all that common code. Each of the \f(CW\*(C`speak\*(C'\fR routines has a | |
268 | similar structure: a \f(CW\*(C`print\*(C'\fR operator and a string that contains | |
269 | common text, except for two of the words. It'd be nice if we could | |
270 | factor out the commonality, in case we decide later to change it all | |
271 | to \f(CW\*(C`says\*(C'\fR instead of \f(CW\*(C`goes\*(C'\fR. | |
272 | .PP | |
273 | And we actually have a way of doing that without much fuss, but we | |
274 | have to hear a bit more about what the method invocation arrow is | |
275 | actually doing for us. | |
276 | .Sh "The extra parameter of method invocation" | |
277 | .IX Subsection "The extra parameter of method invocation" | |
278 | The invocation of: | |
279 | .PP | |
280 | .Vb 1 | |
281 | \& Class->method(@args) | |
282 | .Ve | |
283 | .PP | |
284 | attempts to invoke subroutine \f(CW\*(C`Class::method\*(C'\fR as: | |
285 | .PP | |
286 | .Vb 1 | |
287 | \& Class::method("Class", @args); | |
288 | .Ve | |
289 | .PP | |
290 | (If the subroutine can't be found, \*(L"inheritance\*(R" kicks in, but we'll | |
291 | get to that later.) This means that we get the class name as the | |
292 | first parameter (the only parameter, if no arguments are given). So | |
293 | we can rewrite the \f(CW\*(C`Sheep\*(C'\fR speaking subroutine as: | |
294 | .PP | |
295 | .Vb 4 | |
296 | \& sub Sheep::speak { | |
297 | \& my $class = shift; | |
298 | \& print "a $class goes baaaah!\en"; | |
299 | \& } | |
300 | .Ve | |
301 | .PP | |
302 | And the other two animals come out similarly: | |
303 | .PP | |
304 | .Vb 8 | |
305 | \& sub Cow::speak { | |
306 | \& my $class = shift; | |
307 | \& print "a $class goes moooo!\en"; | |
308 | \& } | |
309 | \& sub Horse::speak { | |
310 | \& my $class = shift; | |
311 | \& print "a $class goes neigh!\en"; | |
312 | \& } | |
313 | .Ve | |
314 | .PP | |
315 | In each case, \f(CW$class\fR will get the value appropriate for that | |
316 | subroutine. But once again, we have a lot of similar structure. Can | |
317 | we factor that out even further? Yes, by calling another method in | |
318 | the same class. | |
319 | .Sh "Calling a second method to simplify things" | |
320 | .IX Subsection "Calling a second method to simplify things" | |
321 | Let's call out from \f(CW\*(C`speak\*(C'\fR to a helper method called \f(CW\*(C`sound\*(C'\fR. | |
322 | This method provides the constant text for the sound itself. | |
323 | .PP | |
324 | .Vb 7 | |
325 | \& { package Cow; | |
326 | \& sub sound { "moooo" } | |
327 | \& sub speak { | |
328 | \& my $class = shift; | |
329 | \& print "a $class goes ", $class->sound, "!\en" | |
330 | \& } | |
331 | \& } | |
332 | .Ve | |
333 | .PP | |
334 | Now, when we call \f(CW\*(C`Cow\->speak\*(C'\fR, we get a \f(CW$class\fR of \f(CW\*(C`Cow\*(C'\fR in | |
335 | \&\f(CW\*(C`speak\*(C'\fR. This in turn selects the \f(CW\*(C`Cow\->sound\*(C'\fR method, which | |
336 | returns \f(CW\*(C`moooo\*(C'\fR. But how different would this be for the \f(CW\*(C`Horse\*(C'\fR? | |
337 | .PP | |
338 | .Vb 7 | |
339 | \& { package Horse; | |
340 | \& sub sound { "neigh" } | |
341 | \& sub speak { | |
342 | \& my $class = shift; | |
343 | \& print "a $class goes ", $class->sound, "!\en" | |
344 | \& } | |
345 | \& } | |
346 | .Ve | |
347 | .PP | |
348 | Only the name of the package and the specific sound change. So can we | |
349 | somehow share the definition for \f(CW\*(C`speak\*(C'\fR between the Cow and the | |
350 | Horse? Yes, with inheritance! | |
351 | .Sh "Inheriting the windpipes" | |
352 | .IX Subsection "Inheriting the windpipes" | |
353 | We'll define a common subroutine package called \f(CW\*(C`Animal\*(C'\fR, with the | |
354 | definition for \f(CW\*(C`speak\*(C'\fR: | |
355 | .PP | |
356 | .Vb 6 | |
357 | \& { package Animal; | |
358 | \& sub speak { | |
359 | \& my $class = shift; | |
360 | \& print "a $class goes ", $class->sound, "!\en" | |
361 | \& } | |
362 | \& } | |
363 | .Ve | |
364 | .PP | |
365 | Then, for each animal, we say it \*(L"inherits\*(R" from \f(CW\*(C`Animal\*(C'\fR, along | |
366 | with the animal-specific sound: | |
367 | .PP | |
368 | .Vb 4 | |
369 | \& { package Cow; | |
370 | \& @ISA = qw(Animal); | |
371 | \& sub sound { "moooo" } | |
372 | \& } | |
373 | .Ve | |
374 | .PP | |
375 | Note the added \f(CW@ISA\fR array. We'll get to that in a minute. | |
376 | .PP | |
377 | But what happens when we invoke \f(CW\*(C`Cow\->speak\*(C'\fR now? | |
378 | .PP | |
379 | First, Perl constructs the argument list. In this case, it's just | |
380 | \&\f(CW\*(C`Cow\*(C'\fR. Then Perl looks for \f(CW\*(C`Cow::speak\*(C'\fR. But that's not there, so | |
381 | Perl checks for the inheritance array \f(CW@Cow::ISA\fR. It's there, | |
382 | and contains the single name \f(CW\*(C`Animal\*(C'\fR. | |
383 | .PP | |
384 | Perl next checks for \f(CW\*(C`speak\*(C'\fR inside \f(CW\*(C`Animal\*(C'\fR instead, as in | |
385 | \&\f(CW\*(C`Animal::speak\*(C'\fR. And that's found, so Perl invokes that subroutine | |
386 | with the already frozen argument list. | |
387 | .PP | |
388 | Inside the \f(CW\*(C`Animal::speak\*(C'\fR subroutine, \f(CW$class\fR becomes \f(CW\*(C`Cow\*(C'\fR (the | |
389 | first argument). So when we get to the step of invoking | |
390 | \&\f(CW\*(C`$class\->sound\*(C'\fR, it'll be looking for \f(CW\*(C`Cow\->sound\*(C'\fR, which | |
391 | gets it on the first try without looking at \f(CW@ISA\fR. Success! | |
392 | .ie n .Sh "A few notes about @ISA" | |
393 | .el .Sh "A few notes about \f(CW@ISA\fP" | |
394 | .IX Subsection "A few notes about @ISA" | |
395 | This magical \f(CW@ISA\fR variable (pronounced \*(L"is a\*(R" not \*(L"ice\-uh\*(R"), has | |
396 | declared that \f(CW\*(C`Cow\*(C'\fR \*(L"is a\*(R" \f(CW\*(C`Animal\*(C'\fR. Note that it's an array, | |
397 | not a simple single value, because on rare occasions, it makes sense | |
398 | to have more than one parent class searched for the missing methods. | |
399 | .PP | |
400 | If \f(CW\*(C`Animal\*(C'\fR also had an \f(CW@ISA\fR, then we'd check there too. The | |
401 | search is recursive, depth\-first, left-to-right in each \f(CW@ISA\fR. | |
402 | Typically, each \f(CW@ISA\fR has only one element (multiple elements means | |
403 | multiple inheritance and multiple headaches), so we get a nice tree of | |
404 | inheritance. | |
405 | .PP | |
406 | When we turn on \f(CW\*(C`use strict\*(C'\fR, we'll get complaints on \f(CW@ISA\fR, since | |
407 | it's not a variable containing an explicit package name, nor is it a | |
408 | lexical (\*(L"my\*(R") variable. We can't make it a lexical variable though | |
409 | (it has to belong to the package to be found by the inheritance mechanism), | |
410 | so there's a couple of straightforward ways to handle that. | |
411 | .PP | |
412 | The easiest is to just spell the package name out: | |
413 | .PP | |
414 | .Vb 1 | |
415 | \& @Cow::ISA = qw(Animal); | |
416 | .Ve | |
417 | .PP | |
418 | Or allow it as an implicitly named package variable: | |
419 | .PP | |
420 | .Vb 3 | |
421 | \& package Cow; | |
422 | \& use vars qw(@ISA); | |
423 | \& @ISA = qw(Animal); | |
424 | .Ve | |
425 | .PP | |
426 | If you're bringing in the class from outside, via an object-oriented | |
427 | module, you change: | |
428 | .PP | |
429 | .Vb 4 | |
430 | \& package Cow; | |
431 | \& use Animal; | |
432 | \& use vars qw(@ISA); | |
433 | \& @ISA = qw(Animal); | |
434 | .Ve | |
435 | .PP | |
436 | into just: | |
437 | .PP | |
438 | .Vb 2 | |
439 | \& package Cow; | |
440 | \& use base qw(Animal); | |
441 | .Ve | |
442 | .PP | |
443 | And that's pretty darn compact. | |
444 | .Sh "Overriding the methods" | |
445 | .IX Subsection "Overriding the methods" | |
446 | Let's add a mouse, which can barely be heard: | |
447 | .PP | |
448 | .Vb 10 | |
449 | \& # Animal package from before | |
450 | \& { package Mouse; | |
451 | \& @ISA = qw(Animal); | |
452 | \& sub sound { "squeak" } | |
453 | \& sub speak { | |
454 | \& my $class = shift; | |
455 | \& print "a $class goes ", $class->sound, "!\en"; | |
456 | \& print "[but you can barely hear it!]\en"; | |
457 | \& } | |
458 | \& } | |
459 | .Ve | |
460 | .PP | |
461 | .Vb 1 | |
462 | \& Mouse->speak; | |
463 | .Ve | |
464 | .PP | |
465 | which results in: | |
466 | .PP | |
467 | .Vb 2 | |
468 | \& a Mouse goes squeak! | |
469 | \& [but you can barely hear it!] | |
470 | .Ve | |
471 | .PP | |
472 | Here, \f(CW\*(C`Mouse\*(C'\fR has its own speaking routine, so \f(CW\*(C`Mouse\->speak\*(C'\fR | |
473 | doesn't immediately invoke \f(CW\*(C`Animal\->speak\*(C'\fR. This is known as | |
474 | \&\*(L"overriding\*(R". In fact, we didn't even need to say that a \f(CW\*(C`Mouse\*(C'\fR was | |
475 | an \f(CW\*(C`Animal\*(C'\fR at all, since all of the methods needed for \f(CW\*(C`speak\*(C'\fR are | |
476 | completely defined with \f(CW\*(C`Mouse\*(C'\fR. | |
477 | .PP | |
478 | But we've now duplicated some of the code from \f(CW\*(C`Animal\->speak\*(C'\fR, | |
479 | and this can once again be a maintenance headache. So, can we avoid | |
480 | that? Can we say somehow that a \f(CW\*(C`Mouse\*(C'\fR does everything any other | |
481 | \&\f(CW\*(C`Animal\*(C'\fR does, but add in the extra comment? Sure! | |
482 | .PP | |
483 | First, we can invoke the \f(CW\*(C`Animal::speak\*(C'\fR method directly: | |
484 | .PP | |
485 | .Vb 10 | |
486 | \& # Animal package from before | |
487 | \& { package Mouse; | |
488 | \& @ISA = qw(Animal); | |
489 | \& sub sound { "squeak" } | |
490 | \& sub speak { | |
491 | \& my $class = shift; | |
492 | \& Animal::speak($class); | |
493 | \& print "[but you can barely hear it!]\en"; | |
494 | \& } | |
495 | \& } | |
496 | .Ve | |
497 | .PP | |
498 | Note that we have to include the \f(CW$class\fR parameter (almost surely | |
499 | the value of \f(CW"Mouse"\fR) as the first parameter to \f(CW\*(C`Animal::speak\*(C'\fR, | |
500 | since we've stopped using the method arrow. Why did we stop? Well, | |
501 | if we invoke \f(CW\*(C`Animal\->speak\*(C'\fR there, the first parameter to the | |
502 | method will be \f(CW"Animal"\fR not \f(CW"Mouse"\fR, and when time comes for it | |
503 | to call for the \f(CW\*(C`sound\*(C'\fR, it won't have the right class to come back | |
504 | to this package. | |
505 | .PP | |
506 | Invoking \f(CW\*(C`Animal::speak\*(C'\fR directly is a mess, however. What if | |
507 | \&\f(CW\*(C`Animal::speak\*(C'\fR didn't exist before, and was being inherited from a | |
508 | class mentioned in \f(CW@Animal::ISA\fR? Because we are no longer using | |
509 | the method arrow, we get one and only one chance to hit the right | |
510 | subroutine. | |
511 | .PP | |
512 | Also note that the \f(CW\*(C`Animal\*(C'\fR classname is now hardwired into the | |
513 | subroutine selection. This is a mess if someone maintains the code, | |
514 | changing \f(CW@ISA\fR for <Mouse> and didn't notice \f(CW\*(C`Animal\*(C'\fR there in | |
515 | \&\f(CW\*(C`speak\*(C'\fR. So, this is probably not the right way to go. | |
516 | .Sh "Starting the search from a different place" | |
517 | .IX Subsection "Starting the search from a different place" | |
518 | A better solution is to tell Perl to search from a higher place | |
519 | in the inheritance chain: | |
520 | .PP | |
521 | .Vb 9 | |
522 | \& # same Animal as before | |
523 | \& { package Mouse; | |
524 | \& # same @ISA, &sound as before | |
525 | \& sub speak { | |
526 | \& my $class = shift; | |
527 | \& $class->Animal::speak; | |
528 | \& print "[but you can barely hear it!]\en"; | |
529 | \& } | |
530 | \& } | |
531 | .Ve | |
532 | .PP | |
533 | Ahh. This works. Using this syntax, we start with \f(CW\*(C`Animal\*(C'\fR to find | |
534 | \&\f(CW\*(C`speak\*(C'\fR, and use all of \f(CW\*(C`Animal\*(C'\fR's inheritance chain if not found | |
535 | immediately. And yet the first parameter will be \f(CW$class\fR, so the | |
536 | found \f(CW\*(C`speak\*(C'\fR method will get \f(CW\*(C`Mouse\*(C'\fR as its first entry, and | |
537 | eventually work its way back to \f(CW\*(C`Mouse::sound\*(C'\fR for the details. | |
538 | .PP | |
539 | But this isn't the best solution. We still have to keep the \f(CW@ISA\fR | |
540 | and the initial search package coordinated. Worse, if \f(CW\*(C`Mouse\*(C'\fR had | |
541 | multiple entries in \f(CW@ISA\fR, we wouldn't necessarily know which one | |
542 | had actually defined \f(CW\*(C`speak\*(C'\fR. So, is there an even better way? | |
543 | .Sh "The \s-1SUPER\s0 way of doing things" | |
544 | .IX Subsection "The SUPER way of doing things" | |
545 | By changing the \f(CW\*(C`Animal\*(C'\fR class to the \f(CW\*(C`SUPER\*(C'\fR class in that | |
546 | invocation, we get a search of all of our super classes (classes | |
547 | listed in \f(CW@ISA\fR) automatically: | |
548 | .PP | |
549 | .Vb 9 | |
550 | \& # same Animal as before | |
551 | \& { package Mouse; | |
552 | \& # same @ISA, &sound as before | |
553 | \& sub speak { | |
554 | \& my $class = shift; | |
555 | \& $class->SUPER::speak; | |
556 | \& print "[but you can barely hear it!]\en"; | |
557 | \& } | |
558 | \& } | |
559 | .Ve | |
560 | .PP | |
561 | So, \f(CW\*(C`SUPER::speak\*(C'\fR means look in the current package's \f(CW@ISA\fR for | |
562 | \&\f(CW\*(C`speak\*(C'\fR, invoking the first one found. Note that it does \fInot\fR look in | |
563 | the \f(CW@ISA\fR of \f(CW$class\fR. | |
564 | .Sh "Where we're at so far..." | |
565 | .IX Subsection "Where we're at so far..." | |
566 | So far, we've seen the method arrow syntax: | |
567 | .PP | |
568 | .Vb 1 | |
569 | \& Class->method(@args); | |
570 | .Ve | |
571 | .PP | |
572 | or the equivalent: | |
573 | .PP | |
574 | .Vb 2 | |
575 | \& $a = "Class"; | |
576 | \& $a->method(@args); | |
577 | .Ve | |
578 | .PP | |
579 | which constructs an argument list of: | |
580 | .PP | |
581 | .Vb 1 | |
582 | \& ("Class", @args) | |
583 | .Ve | |
584 | .PP | |
585 | and attempts to invoke | |
586 | .PP | |
587 | .Vb 1 | |
588 | \& Class::method("Class", @Args); | |
589 | .Ve | |
590 | .PP | |
591 | However, if \f(CW\*(C`Class::method\*(C'\fR is not found, then \f(CW@Class::ISA\fR is examined | |
592 | (recursively) to locate a package that does indeed contain \f(CW\*(C`method\*(C'\fR, | |
593 | and that subroutine is invoked instead. | |
594 | .PP | |
595 | Using this simple syntax, we have class methods, (multiple) | |
596 | inheritance, overriding, and extending. Using just what we've seen so | |
597 | far, we've been able to factor out common code, and provide a nice way | |
598 | to reuse implementations with variations. This is at the core of what | |
599 | objects provide, but objects also provide instance data, which we | |
600 | haven't even begun to cover. | |
601 | .Sh "A horse is a horse, of course of course \*(-- or is it?" | |
602 | .IX Subsection "A horse is a horse, of course of course or is it?" | |
603 | Let's start with the code for the \f(CW\*(C`Animal\*(C'\fR class | |
604 | and the \f(CW\*(C`Horse\*(C'\fR class: | |
605 | .PP | |
606 | .Vb 10 | |
607 | \& { package Animal; | |
608 | \& sub speak { | |
609 | \& my $class = shift; | |
610 | \& print "a $class goes ", $class->sound, "!\en" | |
611 | \& } | |
612 | \& } | |
613 | \& { package Horse; | |
614 | \& @ISA = qw(Animal); | |
615 | \& sub sound { "neigh" } | |
616 | \& } | |
617 | .Ve | |
618 | .PP | |
619 | This lets us invoke \f(CW\*(C`Horse\->speak\*(C'\fR to ripple upward to | |
620 | \&\f(CW\*(C`Animal::speak\*(C'\fR, calling back to \f(CW\*(C`Horse::sound\*(C'\fR to get the specific | |
621 | sound, and the output of: | |
622 | .PP | |
623 | .Vb 1 | |
624 | \& a Horse goes neigh! | |
625 | .Ve | |
626 | .PP | |
627 | But all of our Horse objects would have to be absolutely identical. | |
628 | If I add a subroutine, all horses automatically share it. That's | |
629 | great for making horses the same, but how do we capture the | |
630 | distinctions about an individual horse? For example, suppose I want | |
631 | to give my first horse a name. There's got to be a way to keep its | |
632 | name separate from the other horses. | |
633 | .PP | |
634 | We can do that by drawing a new distinction, called an \*(L"instance\*(R". | |
635 | An \*(L"instance\*(R" is generally created by a class. In Perl, any reference | |
636 | can be an instance, so let's start with the simplest reference | |
637 | that can hold a horse's name: a scalar reference. | |
638 | .PP | |
639 | .Vb 2 | |
640 | \& my $name = "Mr. Ed"; | |
641 | \& my $talking = \e$name; | |
642 | .Ve | |
643 | .PP | |
644 | So now \f(CW$talking\fR is a reference to what will be the instance-specific | |
645 | data (the name). The final step in turning this into a real instance | |
646 | is with a special operator called \f(CW\*(C`bless\*(C'\fR: | |
647 | .PP | |
648 | .Vb 1 | |
649 | \& bless $talking, Horse; | |
650 | .Ve | |
651 | .PP | |
652 | This operator stores information about the package named \f(CW\*(C`Horse\*(C'\fR into | |
653 | the thing pointed at by the reference. At this point, we say | |
654 | \&\f(CW$talking\fR is an instance of \f(CW\*(C`Horse\*(C'\fR. That is, it's a specific | |
655 | horse. The reference is otherwise unchanged, and can still be used | |
656 | with traditional dereferencing operators. | |
657 | .Sh "Invoking an instance method" | |
658 | .IX Subsection "Invoking an instance method" | |
659 | The method arrow can be used on instances, as well as names of | |
660 | packages (classes). So, let's get the sound that \f(CW$talking\fR makes: | |
661 | .PP | |
662 | .Vb 1 | |
663 | \& my $noise = $talking->sound; | |
664 | .Ve | |
665 | .PP | |
666 | To invoke \f(CW\*(C`sound\*(C'\fR, Perl first notes that \f(CW$talking\fR is a blessed | |
667 | reference (and thus an instance). It then constructs an argument | |
668 | list, in this case from just \f(CW\*(C`($talking)\*(C'\fR. (Later we'll see that | |
669 | arguments will take their place following the instance variable, | |
670 | just like with classes.) | |
671 | .PP | |
672 | Now for the fun part: Perl takes the class in which the instance was | |
673 | blessed, in this case \f(CW\*(C`Horse\*(C'\fR, and uses that to locate the subroutine | |
674 | to invoke the method. In this case, \f(CW\*(C`Horse::sound\*(C'\fR is found directly | |
675 | (without using inheritance), yielding the final subroutine invocation: | |
676 | .PP | |
677 | .Vb 1 | |
678 | \& Horse::sound($talking) | |
679 | .Ve | |
680 | .PP | |
681 | Note that the first parameter here is still the instance, not the name | |
682 | of the class as before. We'll get \f(CW\*(C`neigh\*(C'\fR as the return value, and | |
683 | that'll end up as the \f(CW$noise\fR variable above. | |
684 | .PP | |
685 | If Horse::sound had not been found, we'd be wandering up the | |
686 | \&\f(CW@Horse::ISA\fR list to try to find the method in one of the | |
687 | superclasses, just as for a class method. The only difference between | |
688 | a class method and an instance method is whether the first parameter | |
689 | is an instance (a blessed reference) or a class name (a string). | |
690 | .Sh "Accessing the instance data" | |
691 | .IX Subsection "Accessing the instance data" | |
692 | Because we get the instance as the first parameter, we can now access | |
693 | the instance-specific data. In this case, let's add a way to get at | |
694 | the name: | |
695 | .PP | |
696 | .Vb 8 | |
697 | \& { package Horse; | |
698 | \& @ISA = qw(Animal); | |
699 | \& sub sound { "neigh" } | |
700 | \& sub name { | |
701 | \& my $self = shift; | |
702 | \& $$self; | |
703 | \& } | |
704 | \& } | |
705 | .Ve | |
706 | .PP | |
707 | Now we call for the name: | |
708 | .PP | |
709 | .Vb 1 | |
710 | \& print $talking->name, " says ", $talking->sound, "\en"; | |
711 | .Ve | |
712 | .PP | |
713 | Inside \f(CW\*(C`Horse::name\*(C'\fR, the \f(CW@_\fR array contains just \f(CW$talking\fR, | |
714 | which the \f(CW\*(C`shift\*(C'\fR stores into \f(CW$self\fR. (It's traditional to shift | |
715 | the first parameter off into a variable named \f(CW$self\fR for instance | |
716 | methods, so stay with that unless you have strong reasons otherwise.) | |
717 | Then, \f(CW$self\fR gets de-referenced as a scalar ref, yielding \f(CW\*(C`Mr. Ed\*(C'\fR, | |
718 | and we're done with that. The result is: | |
719 | .PP | |
720 | .Vb 1 | |
721 | \& Mr. Ed says neigh. | |
722 | .Ve | |
723 | .Sh "How to build a horse" | |
724 | .IX Subsection "How to build a horse" | |
725 | Of course, if we constructed all of our horses by hand, we'd most | |
726 | likely make mistakes from time to time. We're also violating one of | |
727 | the properties of object-oriented programming, in that the \*(L"inside | |
728 | guts\*(R" of a Horse are visible. That's good if you're a veterinarian, | |
729 | but not if you just like to own horses. So, let's let the Horse class | |
730 | build a new horse: | |
731 | .PP | |
732 | .Vb 13 | |
733 | \& { package Horse; | |
734 | \& @ISA = qw(Animal); | |
735 | \& sub sound { "neigh" } | |
736 | \& sub name { | |
737 | \& my $self = shift; | |
738 | \& $$self; | |
739 | \& } | |
740 | \& sub named { | |
741 | \& my $class = shift; | |
742 | \& my $name = shift; | |
743 | \& bless \e$name, $class; | |
744 | \& } | |
745 | \& } | |
746 | .Ve | |
747 | .PP | |
748 | Now with the new \f(CW\*(C`named\*(C'\fR method, we can build a horse: | |
749 | .PP | |
750 | .Vb 1 | |
751 | \& my $talking = Horse->named("Mr. Ed"); | |
752 | .Ve | |
753 | .PP | |
754 | Notice we're back to a class method, so the two arguments to | |
755 | \&\f(CW\*(C`Horse::named\*(C'\fR are \f(CW\*(C`Horse\*(C'\fR and \f(CW\*(C`Mr. Ed\*(C'\fR. The \f(CW\*(C`bless\*(C'\fR operator | |
756 | not only blesses \f(CW$name\fR, it also returns the reference to \f(CW$name\fR, | |
757 | so that's fine as a return value. And that's how to build a horse. | |
758 | .PP | |
759 | We've called the constructor \f(CW\*(C`named\*(C'\fR here, so that it quickly denotes | |
760 | the constructor's argument as the name for this particular \f(CW\*(C`Horse\*(C'\fR. | |
761 | You can use different constructors with different names for different | |
762 | ways of \*(L"giving birth\*(R" to the object (like maybe recording its | |
763 | pedigree or date of birth). However, you'll find that most people | |
764 | coming to Perl from more limited languages use a single constructor | |
765 | named \f(CW\*(C`new\*(C'\fR, with various ways of interpreting the arguments to | |
766 | \&\f(CW\*(C`new\*(C'\fR. Either style is fine, as long as you document your particular | |
767 | way of giving birth to an object. (And you \fIwere\fR going to do that, | |
768 | right?) | |
769 | .Sh "Inheriting the constructor" | |
770 | .IX Subsection "Inheriting the constructor" | |
771 | But was there anything specific to \f(CW\*(C`Horse\*(C'\fR in that method? No. Therefore, | |
772 | it's also the same recipe for building anything else that inherited from | |
773 | \&\f(CW\*(C`Animal\*(C'\fR, so let's put it there: | |
774 | .PP | |
775 | .Vb 19 | |
776 | \& { package Animal; | |
777 | \& sub speak { | |
778 | \& my $class = shift; | |
779 | \& print "a $class goes ", $class->sound, "!\en" | |
780 | \& } | |
781 | \& sub name { | |
782 | \& my $self = shift; | |
783 | \& $$self; | |
784 | \& } | |
785 | \& sub named { | |
786 | \& my $class = shift; | |
787 | \& my $name = shift; | |
788 | \& bless \e$name, $class; | |
789 | \& } | |
790 | \& } | |
791 | \& { package Horse; | |
792 | \& @ISA = qw(Animal); | |
793 | \& sub sound { "neigh" } | |
794 | \& } | |
795 | .Ve | |
796 | .PP | |
797 | Ahh, but what happens if we invoke \f(CW\*(C`speak\*(C'\fR on an instance? | |
798 | .PP | |
799 | .Vb 2 | |
800 | \& my $talking = Horse->named("Mr. Ed"); | |
801 | \& $talking->speak; | |
802 | .Ve | |
803 | .PP | |
804 | We get a debugging value: | |
805 | .PP | |
806 | .Vb 1 | |
807 | \& a Horse=SCALAR(0xaca42ac) goes neigh! | |
808 | .Ve | |
809 | .PP | |
810 | Why? Because the \f(CW\*(C`Animal::speak\*(C'\fR routine is expecting a classname as | |
811 | its first parameter, not an instance. When the instance is passed in, | |
812 | we'll end up using a blessed scalar reference as a string, and that | |
813 | shows up as we saw it just now. | |
814 | .Sh "Making a method work with either classes or instances" | |
815 | .IX Subsection "Making a method work with either classes or instances" | |
816 | All we need is for a method to detect if it is being called on a class | |
817 | or called on an instance. The most straightforward way is with the | |
818 | \&\f(CW\*(C`ref\*(C'\fR operator. This returns a string (the classname) when used on a | |
819 | blessed reference, and \f(CW\*(C`undef\*(C'\fR when used on a string (like a | |
820 | classname). Let's modify the \f(CW\*(C`name\*(C'\fR method first to notice the change: | |
821 | .PP | |
822 | .Vb 6 | |
823 | \& sub name { | |
824 | \& my $either = shift; | |
825 | \& ref $either | |
826 | \& ? $$either # it's an instance, return name | |
827 | \& : "an unnamed $either"; # it's a class, return generic | |
828 | \& } | |
829 | .Ve | |
830 | .PP | |
831 | Here, the \f(CW\*(C`?:\*(C'\fR operator comes in handy to select either the | |
832 | dereference or a derived string. Now we can use this with either an | |
833 | instance or a class. Note that I've changed the first parameter | |
834 | holder to \f(CW$either\fR to show that this is intended: | |
835 | .PP | |
836 | .Vb 3 | |
837 | \& my $talking = Horse->named("Mr. Ed"); | |
838 | \& print Horse->name, "\en"; # prints "an unnamed Horse\en" | |
839 | \& print $talking->name, "\en"; # prints "Mr Ed.\en" | |
840 | .Ve | |
841 | .PP | |
842 | and now we'll fix \f(CW\*(C`speak\*(C'\fR to use this: | |
843 | .PP | |
844 | .Vb 4 | |
845 | \& sub speak { | |
846 | \& my $either = shift; | |
847 | \& print $either->name, " goes ", $either->sound, "\en"; | |
848 | \& } | |
849 | .Ve | |
850 | .PP | |
851 | And since \f(CW\*(C`sound\*(C'\fR already worked with either a class or an instance, | |
852 | we're done! | |
853 | .Sh "Adding parameters to a method" | |
854 | .IX Subsection "Adding parameters to a method" | |
855 | Let's train our animals to eat: | |
856 | .PP | |
857 | .Vb 30 | |
858 | \& { package Animal; | |
859 | \& sub named { | |
860 | \& my $class = shift; | |
861 | \& my $name = shift; | |
862 | \& bless \e$name, $class; | |
863 | \& } | |
864 | \& sub name { | |
865 | \& my $either = shift; | |
866 | \& ref $either | |
867 | \& ? $$either # it's an instance, return name | |
868 | \& : "an unnamed $either"; # it's a class, return generic | |
869 | \& } | |
870 | \& sub speak { | |
871 | \& my $either = shift; | |
872 | \& print $either->name, " goes ", $either->sound, "\en"; | |
873 | \& } | |
874 | \& sub eat { | |
875 | \& my $either = shift; | |
876 | \& my $food = shift; | |
877 | \& print $either->name, " eats $food.\en"; | |
878 | \& } | |
879 | \& } | |
880 | \& { package Horse; | |
881 | \& @ISA = qw(Animal); | |
882 | \& sub sound { "neigh" } | |
883 | \& } | |
884 | \& { package Sheep; | |
885 | \& @ISA = qw(Animal); | |
886 | \& sub sound { "baaaah" } | |
887 | \& } | |
888 | .Ve | |
889 | .PP | |
890 | And now try it out: | |
891 | .PP | |
892 | .Vb 3 | |
893 | \& my $talking = Horse->named("Mr. Ed"); | |
894 | \& $talking->eat("hay"); | |
895 | \& Sheep->eat("grass"); | |
896 | .Ve | |
897 | .PP | |
898 | which prints: | |
899 | .PP | |
900 | .Vb 2 | |
901 | \& Mr. Ed eats hay. | |
902 | \& an unnamed Sheep eats grass. | |
903 | .Ve | |
904 | .PP | |
905 | An instance method with parameters gets invoked with the instance, | |
906 | and then the list of parameters. So that first invocation is like: | |
907 | .PP | |
908 | .Vb 1 | |
909 | \& Animal::eat($talking, "hay"); | |
910 | .Ve | |
911 | .Sh "More interesting instances" | |
912 | .IX Subsection "More interesting instances" | |
913 | What if an instance needs more data? Most interesting instances are | |
914 | made of many items, each of which can in turn be a reference or even | |
915 | another object. The easiest way to store these is often in a hash. | |
916 | The keys of the hash serve as the names of parts of the object (often | |
917 | called \*(L"instance variables\*(R" or \*(L"member variables\*(R"), and the | |
918 | corresponding values are, well, the values. | |
919 | .PP | |
920 | But how do we turn the horse into a hash? Recall that an object was | |
921 | any blessed reference. We can just as easily make it a blessed hash | |
922 | reference as a blessed scalar reference, as long as everything that | |
923 | looks at the reference is changed accordingly. | |
924 | .PP | |
925 | Let's make a sheep that has a name and a color: | |
926 | .PP | |
927 | .Vb 1 | |
928 | \& my $bad = bless { Name => "Evil", Color => "black" }, Sheep; | |
929 | .Ve | |
930 | .PP | |
931 | so \f(CW\*(C`$bad\->{Name}\*(C'\fR has \f(CW\*(C`Evil\*(C'\fR, and \f(CW\*(C`$bad\->{Color}\*(C'\fR has | |
932 | \&\f(CW\*(C`black\*(C'\fR. But we want to make \f(CW\*(C`$bad\->name\*(C'\fR access the name, and | |
933 | that's now messed up because it's expecting a scalar reference. Not | |
934 | to worry, because that's pretty easy to fix up: | |
935 | .PP | |
936 | .Vb 7 | |
937 | \& ## in Animal | |
938 | \& sub name { | |
939 | \& my $either = shift; | |
940 | \& ref $either ? | |
941 | \& $either->{Name} : | |
942 | \& "an unnamed $either"; | |
943 | \& } | |
944 | .Ve | |
945 | .PP | |
946 | And of course \f(CW\*(C`named\*(C'\fR still builds a scalar sheep, so let's fix that | |
947 | as well: | |
948 | .PP | |
949 | .Vb 7 | |
950 | \& ## in Animal | |
951 | \& sub named { | |
952 | \& my $class = shift; | |
953 | \& my $name = shift; | |
954 | \& my $self = { Name => $name, Color => $class->default_color }; | |
955 | \& bless $self, $class; | |
956 | \& } | |
957 | .Ve | |
958 | .PP | |
959 | What's this \f(CW\*(C`default_color\*(C'\fR? Well, if \f(CW\*(C`named\*(C'\fR has only the name, | |
960 | we still need to set a color, so we'll have a class-specific initial color. | |
961 | For a sheep, we might define it as white: | |
962 | .PP | |
963 | .Vb 2 | |
964 | \& ## in Sheep | |
965 | \& sub default_color { "white" } | |
966 | .Ve | |
967 | .PP | |
968 | And then to keep from having to define one for each additional class, | |
969 | we'll define a \*(L"backstop\*(R" method that serves as the \*(L"default default\*(R", | |
970 | directly in \f(CW\*(C`Animal\*(C'\fR: | |
971 | .PP | |
972 | .Vb 2 | |
973 | \& ## in Animal | |
974 | \& sub default_color { "brown" } | |
975 | .Ve | |
976 | .PP | |
977 | Now, because \f(CW\*(C`name\*(C'\fR and \f(CW\*(C`named\*(C'\fR were the only methods that | |
978 | referenced the \*(L"structure\*(R" of the object, the rest of the methods can | |
979 | remain the same, so \f(CW\*(C`speak\*(C'\fR still works as before. | |
980 | .Sh "A horse of a different color" | |
981 | .IX Subsection "A horse of a different color" | |
982 | But having all our horses be brown would be boring. So let's add a | |
983 | method or two to get and set the color. | |
984 | .PP | |
985 | .Vb 7 | |
986 | \& ## in Animal | |
987 | \& sub color { | |
988 | \& $_[0]->{Color} | |
989 | \& } | |
990 | \& sub set_color { | |
991 | \& $_[0]->{Color} = $_[1]; | |
992 | \& } | |
993 | .Ve | |
994 | .PP | |
995 | Note the alternate way of accessing the arguments: \f(CW$_[0]\fR is used | |
996 | in\-place, rather than with a \f(CW\*(C`shift\*(C'\fR. (This saves us a bit of time | |
997 | for something that may be invoked frequently.) And now we can fix | |
998 | that color for Mr. Ed: | |
999 | .PP | |
1000 | .Vb 3 | |
1001 | \& my $talking = Horse->named("Mr. Ed"); | |
1002 | \& $talking->set_color("black-and-white"); | |
1003 | \& print $talking->name, " is colored ", $talking->color, "\en"; | |
1004 | .Ve | |
1005 | .PP | |
1006 | which results in: | |
1007 | .PP | |
1008 | .Vb 1 | |
1009 | \& Mr. Ed is colored black-and-white | |
1010 | .Ve | |
1011 | .Sh "Summary" | |
1012 | .IX Subsection "Summary" | |
1013 | So, now we have class methods, constructors, instance methods, | |
1014 | instance data, and even accessors. But that's still just the | |
1015 | beginning of what Perl has to offer. We haven't even begun to talk | |
1016 | about accessors that double as getters and setters, destructors, | |
1017 | indirect object notation, subclasses that add instance data, per-class | |
1018 | data, overloading, \*(L"isa\*(R" and \*(L"can\*(R" tests, \f(CW\*(C`UNIVERSAL\*(C'\fR class, and so | |
1019 | on. That's for the rest of the Perl documentation to cover. | |
1020 | Hopefully, this gets you started, though. | |
1021 | .SH "SEE ALSO" | |
1022 | .IX Header "SEE ALSO" | |
1023 | For more information, see perlobj (for all the gritty details about | |
1024 | Perl objects, now that you've seen the basics), perltoot (the | |
1025 | tutorial for those who already know objects), perltooc (dealing | |
1026 | with class data), perlbot (for some more tricks), and books such as | |
1027 | Damian Conway's excellent \fIObject Oriented Perl\fR. | |
1028 | .PP | |
1029 | Some modules which might prove interesting are Class::Accessor, | |
1030 | Class::Class, Class::Contract, Class::Data::Inheritable, | |
1031 | Class::MethodMaker and Tie::SecureHash | |
1032 | .SH "COPYRIGHT" | |
1033 | .IX Header "COPYRIGHT" | |
1034 | Copyright (c) 1999, 2000 by Randal L. Schwartz and Stonehenge | |
1035 | Consulting Services, Inc. Permission is hereby granted to distribute | |
1036 | this document intact with the Perl distribution, and in accordance | |
1037 | with the licenses of the Perl distribution; derived documents must | |
1038 | include this copyright notice intact. | |
1039 | .PP | |
1040 | Portions of this text have been derived from Perl Training materials | |
1041 | originally appearing in the \fIPackages, References, Objects, and | |
1042 | Modules\fR course taught by instructors for Stonehenge Consulting | |
1043 | Services, Inc. and used with permission. | |
1044 | .PP | |
1045 | Portions of this text have been derived from materials originally | |
1046 | appearing in \fILinux Magazine\fR and used with permission. |