Commit | Line | Data |
---|---|---|
86530b38 AT |
1 | .\" Automatically generated by Pod::Man v1.34, Pod::Parser v1.13 |
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 "PERLBOT 1" | |
132 | .TH PERLBOT 1 "2002-06-08" "perl v5.8.0" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlbot \- Bag'o Object Tricks (the BOT) | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | The following collection of tricks and hints is intended to whet curious | |
138 | appetites about such things as the use of instance variables and the | |
139 | mechanics of object and class relationships. The reader is encouraged to | |
140 | consult relevant textbooks for discussion of Object Oriented definitions and | |
141 | methodology. This is not intended as a tutorial for object-oriented | |
142 | programming or as a comprehensive guide to Perl's object oriented features, | |
143 | nor should it be construed as a style guide. | |
144 | .PP | |
145 | The Perl motto still holds: There's more than one way to do it. | |
146 | .SH "OO SCALING TIPS" | |
147 | .IX Header "OO SCALING TIPS" | |
148 | .IP "1" 5 | |
149 | .IX Item "1" | |
150 | Do not attempt to verify the type of \f(CW$self\fR. That'll break if the class is | |
151 | inherited, when the type of \f(CW$self\fR is valid but its package isn't what you | |
152 | expect. See rule 5. | |
153 | .IP "2" 5 | |
154 | .IX Item "2" | |
155 | If an object-oriented (\s-1OO\s0) or indirect-object (\s-1IO\s0) syntax was used, then the | |
156 | object is probably the correct type and there's no need to become paranoid | |
157 | about it. Perl isn't a paranoid language anyway. If people subvert the \s-1OO\s0 | |
158 | or \s-1IO\s0 syntax then they probably know what they're doing and you should let | |
159 | them do it. See rule 1. | |
160 | .IP "3" 5 | |
161 | .IX Item "3" | |
162 | Use the two-argument form of \fIbless()\fR. Let a subclass use your constructor. | |
163 | See \*(L"\s-1INHERITING\s0 A \s-1CONSTRUCTOR\s0\*(R". | |
164 | .IP "4" 5 | |
165 | .IX Item "4" | |
166 | The subclass is allowed to know things about its immediate superclass, the | |
167 | superclass is allowed to know nothing about a subclass. | |
168 | .IP "5" 5 | |
169 | .IX Item "5" | |
170 | Don't be trigger happy with inheritance. A \*(L"using\*(R", \*(L"containing\*(R", or | |
171 | \&\*(L"delegation\*(R" relationship (some sort of aggregation, at least) is often more | |
172 | appropriate. See \*(L"\s-1OBJECT\s0 \s-1RELATIONSHIPS\s0\*(R", \*(L"\s-1USING\s0 \s-1RELATIONSHIP\s0 \s-1WITH\s0 \s-1SDBM\s0\*(R", | |
173 | and \*(L"\s-1DELEGATION\s0\*(R". | |
174 | .IP "6" 5 | |
175 | .IX Item "6" | |
176 | The object is the namespace. Make package globals accessible via the | |
177 | object. This will remove the guess work about the symbol's home package. | |
178 | See \*(L"\s-1CLASS\s0 \s-1CONTEXT\s0 \s-1AND\s0 \s-1THE\s0 \s-1OBJECT\s0\*(R". | |
179 | .IP "7" 5 | |
180 | .IX Item "7" | |
181 | \&\s-1IO\s0 syntax is certainly less noisy, but it is also prone to ambiguities that | |
182 | can cause difficult-to-find bugs. Allow people to use the sure-thing \s-1OO\s0 | |
183 | syntax, even if you don't like it. | |
184 | .IP "8" 5 | |
185 | .IX Item "8" | |
186 | Do not use function-call syntax on a method. You're going to be bitten | |
187 | someday. Someone might move that method into a superclass and your code | |
188 | will be broken. On top of that you're feeding the paranoia in rule 2. | |
189 | .IP "9" 5 | |
190 | .IX Item "9" | |
191 | Don't assume you know the home package of a method. You're making it | |
192 | difficult for someone to override that method. See \*(L"\s-1THINKING\s0 \s-1OF\s0 \s-1CODE\s0 \s-1REUSE\s0\*(R". | |
193 | .SH "INSTANCE VARIABLES" | |
194 | .IX Header "INSTANCE VARIABLES" | |
195 | An anonymous array or anonymous hash can be used to hold instance | |
196 | variables. Named parameters are also demonstrated. | |
197 | .PP | |
198 | .Vb 1 | |
199 | \& package Foo; | |
200 | .Ve | |
201 | .PP | |
202 | .Vb 8 | |
203 | \& sub new { | |
204 | \& my $type = shift; | |
205 | \& my %params = @_; | |
206 | \& my $self = {}; | |
207 | \& $self->{'High'} = $params{'High'}; | |
208 | \& $self->{'Low'} = $params{'Low'}; | |
209 | \& bless $self, $type; | |
210 | \& } | |
211 | .Ve | |
212 | .PP | |
213 | .Vb 1 | |
214 | \& package Bar; | |
215 | .Ve | |
216 | .PP | |
217 | .Vb 8 | |
218 | \& sub new { | |
219 | \& my $type = shift; | |
220 | \& my %params = @_; | |
221 | \& my $self = []; | |
222 | \& $self->[0] = $params{'Left'}; | |
223 | \& $self->[1] = $params{'Right'}; | |
224 | \& bless $self, $type; | |
225 | \& } | |
226 | .Ve | |
227 | .PP | |
228 | .Vb 1 | |
229 | \& package main; | |
230 | .Ve | |
231 | .PP | |
232 | .Vb 3 | |
233 | \& $a = Foo->new( 'High' => 42, 'Low' => 11 ); | |
234 | \& print "High=$a->{'High'}\en"; | |
235 | \& print "Low=$a->{'Low'}\en"; | |
236 | .Ve | |
237 | .PP | |
238 | .Vb 3 | |
239 | \& $b = Bar->new( 'Left' => 78, 'Right' => 40 ); | |
240 | \& print "Left=$b->[0]\en"; | |
241 | \& print "Right=$b->[1]\en"; | |
242 | .Ve | |
243 | .SH "SCALAR INSTANCE VARIABLES" | |
244 | .IX Header "SCALAR INSTANCE VARIABLES" | |
245 | An anonymous scalar can be used when only one instance variable is needed. | |
246 | .PP | |
247 | .Vb 1 | |
248 | \& package Foo; | |
249 | .Ve | |
250 | .PP | |
251 | .Vb 6 | |
252 | \& sub new { | |
253 | \& my $type = shift; | |
254 | \& my $self; | |
255 | \& $self = shift; | |
256 | \& bless \e$self, $type; | |
257 | \& } | |
258 | .Ve | |
259 | .PP | |
260 | .Vb 1 | |
261 | \& package main; | |
262 | .Ve | |
263 | .PP | |
264 | .Vb 2 | |
265 | \& $a = Foo->new( 42 ); | |
266 | \& print "a=$$a\en"; | |
267 | .Ve | |
268 | .SH "INSTANCE VARIABLE INHERITANCE" | |
269 | .IX Header "INSTANCE VARIABLE INHERITANCE" | |
270 | This example demonstrates how one might inherit instance variables from a | |
271 | superclass for inclusion in the new class. This requires calling the | |
272 | superclass's constructor and adding one's own instance variables to the new | |
273 | object. | |
274 | .PP | |
275 | .Vb 1 | |
276 | \& package Bar; | |
277 | .Ve | |
278 | .PP | |
279 | .Vb 6 | |
280 | \& sub new { | |
281 | \& my $type = shift; | |
282 | \& my $self = {}; | |
283 | \& $self->{'buz'} = 42; | |
284 | \& bless $self, $type; | |
285 | \& } | |
286 | .Ve | |
287 | .PP | |
288 | .Vb 2 | |
289 | \& package Foo; | |
290 | \& @ISA = qw( Bar ); | |
291 | .Ve | |
292 | .PP | |
293 | .Vb 6 | |
294 | \& sub new { | |
295 | \& my $type = shift; | |
296 | \& my $self = Bar->new; | |
297 | \& $self->{'biz'} = 11; | |
298 | \& bless $self, $type; | |
299 | \& } | |
300 | .Ve | |
301 | .PP | |
302 | .Vb 1 | |
303 | \& package main; | |
304 | .Ve | |
305 | .PP | |
306 | .Vb 3 | |
307 | \& $a = Foo->new; | |
308 | \& print "buz = ", $a->{'buz'}, "\en"; | |
309 | \& print "biz = ", $a->{'biz'}, "\en"; | |
310 | .Ve | |
311 | .SH "OBJECT RELATIONSHIPS" | |
312 | .IX Header "OBJECT RELATIONSHIPS" | |
313 | The following demonstrates how one might implement \*(L"containing\*(R" and \*(L"using\*(R" | |
314 | relationships between objects. | |
315 | .PP | |
316 | .Vb 1 | |
317 | \& package Bar; | |
318 | .Ve | |
319 | .PP | |
320 | .Vb 6 | |
321 | \& sub new { | |
322 | \& my $type = shift; | |
323 | \& my $self = {}; | |
324 | \& $self->{'buz'} = 42; | |
325 | \& bless $self, $type; | |
326 | \& } | |
327 | .Ve | |
328 | .PP | |
329 | .Vb 1 | |
330 | \& package Foo; | |
331 | .Ve | |
332 | .PP | |
333 | .Vb 7 | |
334 | \& sub new { | |
335 | \& my $type = shift; | |
336 | \& my $self = {}; | |
337 | \& $self->{'Bar'} = Bar->new; | |
338 | \& $self->{'biz'} = 11; | |
339 | \& bless $self, $type; | |
340 | \& } | |
341 | .Ve | |
342 | .PP | |
343 | .Vb 1 | |
344 | \& package main; | |
345 | .Ve | |
346 | .PP | |
347 | .Vb 3 | |
348 | \& $a = Foo->new; | |
349 | \& print "buz = ", $a->{'Bar'}->{'buz'}, "\en"; | |
350 | \& print "biz = ", $a->{'biz'}, "\en"; | |
351 | .Ve | |
352 | .SH "OVERRIDING SUPERCLASS METHODS" | |
353 | .IX Header "OVERRIDING SUPERCLASS METHODS" | |
354 | The following example demonstrates how to override a superclass method and | |
355 | then call the overridden method. The \fB\s-1SUPER\s0\fR pseudo-class allows the | |
356 | programmer to call an overridden superclass method without actually knowing | |
357 | where that method is defined. | |
358 | .PP | |
359 | .Vb 2 | |
360 | \& package Buz; | |
361 | \& sub goo { print "here's the goo\en" } | |
362 | .Ve | |
363 | .PP | |
364 | .Vb 2 | |
365 | \& package Bar; @ISA = qw( Buz ); | |
366 | \& sub google { print "google here\en" } | |
367 | .Ve | |
368 | .PP | |
369 | .Vb 2 | |
370 | \& package Baz; | |
371 | \& sub mumble { print "mumbling\en" } | |
372 | .Ve | |
373 | .PP | |
374 | .Vb 2 | |
375 | \& package Foo; | |
376 | \& @ISA = qw( Bar Baz ); | |
377 | .Ve | |
378 | .PP | |
379 | .Vb 17 | |
380 | \& sub new { | |
381 | \& my $type = shift; | |
382 | \& bless [], $type; | |
383 | \& } | |
384 | \& sub grr { print "grumble\en" } | |
385 | \& sub goo { | |
386 | \& my $self = shift; | |
387 | \& $self->SUPER::goo(); | |
388 | \& } | |
389 | \& sub mumble { | |
390 | \& my $self = shift; | |
391 | \& $self->SUPER::mumble(); | |
392 | \& } | |
393 | \& sub google { | |
394 | \& my $self = shift; | |
395 | \& $self->SUPER::google(); | |
396 | \& } | |
397 | .Ve | |
398 | .PP | |
399 | .Vb 1 | |
400 | \& package main; | |
401 | .Ve | |
402 | .PP | |
403 | .Vb 5 | |
404 | \& $foo = Foo->new; | |
405 | \& $foo->mumble; | |
406 | \& $foo->grr; | |
407 | \& $foo->goo; | |
408 | \& $foo->google; | |
409 | .Ve | |
410 | .SH "USING RELATIONSHIP WITH SDBM" | |
411 | .IX Header "USING RELATIONSHIP WITH SDBM" | |
412 | This example demonstrates an interface for the \s-1SDBM\s0 class. This creates a | |
413 | \&\*(L"using\*(R" relationship between the \s-1SDBM\s0 class and the new class Mydbm. | |
414 | .PP | |
415 | .Vb 1 | |
416 | \& package Mydbm; | |
417 | .Ve | |
418 | .PP | |
419 | .Vb 3 | |
420 | \& require SDBM_File; | |
421 | \& require Tie::Hash; | |
422 | \& @ISA = qw( Tie::Hash ); | |
423 | .Ve | |
424 | .PP | |
425 | .Vb 19 | |
426 | \& sub TIEHASH { | |
427 | \& my $type = shift; | |
428 | \& my $ref = SDBM_File->new(@_); | |
429 | \& bless {'dbm' => $ref}, $type; | |
430 | \& } | |
431 | \& sub FETCH { | |
432 | \& my $self = shift; | |
433 | \& my $ref = $self->{'dbm'}; | |
434 | \& $ref->FETCH(@_); | |
435 | \& } | |
436 | \& sub STORE { | |
437 | \& my $self = shift; | |
438 | \& if (defined $_[0]){ | |
439 | \& my $ref = $self->{'dbm'}; | |
440 | \& $ref->STORE(@_); | |
441 | \& } else { | |
442 | \& die "Cannot STORE an undefined key in Mydbm\en"; | |
443 | \& } | |
444 | \& } | |
445 | .Ve | |
446 | .PP | |
447 | .Vb 2 | |
448 | \& package main; | |
449 | \& use Fcntl qw( O_RDWR O_CREAT ); | |
450 | .Ve | |
451 | .PP | |
452 | .Vb 3 | |
453 | \& tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; | |
454 | \& $foo{'bar'} = 123; | |
455 | \& print "foo-bar = $foo{'bar'}\en"; | |
456 | .Ve | |
457 | .PP | |
458 | .Vb 3 | |
459 | \& tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; | |
460 | \& $bar{'Cathy'} = 456; | |
461 | \& print "bar-Cathy = $bar{'Cathy'}\en"; | |
462 | .Ve | |
463 | .SH "THINKING OF CODE REUSE" | |
464 | .IX Header "THINKING OF CODE REUSE" | |
465 | One strength of Object-Oriented languages is the ease with which old code | |
466 | can use new code. The following examples will demonstrate first how one can | |
467 | hinder code reuse and then how one can promote code reuse. | |
468 | .PP | |
469 | This first example illustrates a class which uses a fully-qualified method | |
470 | call to access the \*(L"private\*(R" method \s-1\fIBAZ\s0()\fR. The second example will show | |
471 | that it is impossible to override the \s-1\fIBAZ\s0()\fR method. | |
472 | .PP | |
473 | .Vb 1 | |
474 | \& package FOO; | |
475 | .Ve | |
476 | .PP | |
477 | .Vb 8 | |
478 | \& sub new { | |
479 | \& my $type = shift; | |
480 | \& bless {}, $type; | |
481 | \& } | |
482 | \& sub bar { | |
483 | \& my $self = shift; | |
484 | \& $self->FOO::private::BAZ; | |
485 | \& } | |
486 | .Ve | |
487 | .PP | |
488 | .Vb 1 | |
489 | \& package FOO::private; | |
490 | .Ve | |
491 | .PP | |
492 | .Vb 3 | |
493 | \& sub BAZ { | |
494 | \& print "in BAZ\en"; | |
495 | \& } | |
496 | .Ve | |
497 | .PP | |
498 | .Vb 1 | |
499 | \& package main; | |
500 | .Ve | |
501 | .PP | |
502 | .Vb 2 | |
503 | \& $a = FOO->new; | |
504 | \& $a->bar; | |
505 | .Ve | |
506 | .PP | |
507 | Now we try to override the \s-1\fIBAZ\s0()\fR method. We would like \fIFOO::bar()\fR to call | |
508 | \&\s-1\fIGOOP::BAZ\s0()\fR, but this cannot happen because \fIFOO::bar()\fR explicitly calls | |
509 | \&\fIFOO::private::BAZ()\fR. | |
510 | .PP | |
511 | .Vb 1 | |
512 | \& package FOO; | |
513 | .Ve | |
514 | .PP | |
515 | .Vb 8 | |
516 | \& sub new { | |
517 | \& my $type = shift; | |
518 | \& bless {}, $type; | |
519 | \& } | |
520 | \& sub bar { | |
521 | \& my $self = shift; | |
522 | \& $self->FOO::private::BAZ; | |
523 | \& } | |
524 | .Ve | |
525 | .PP | |
526 | .Vb 1 | |
527 | \& package FOO::private; | |
528 | .Ve | |
529 | .PP | |
530 | .Vb 3 | |
531 | \& sub BAZ { | |
532 | \& print "in BAZ\en"; | |
533 | \& } | |
534 | .Ve | |
535 | .PP | |
536 | .Vb 6 | |
537 | \& package GOOP; | |
538 | \& @ISA = qw( FOO ); | |
539 | \& sub new { | |
540 | \& my $type = shift; | |
541 | \& bless {}, $type; | |
542 | \& } | |
543 | .Ve | |
544 | .PP | |
545 | .Vb 3 | |
546 | \& sub BAZ { | |
547 | \& print "in GOOP::BAZ\en"; | |
548 | \& } | |
549 | .Ve | |
550 | .PP | |
551 | .Vb 1 | |
552 | \& package main; | |
553 | .Ve | |
554 | .PP | |
555 | .Vb 2 | |
556 | \& $a = GOOP->new; | |
557 | \& $a->bar; | |
558 | .Ve | |
559 | .PP | |
560 | To create reusable code we must modify class \s-1FOO\s0, flattening class | |
561 | FOO::private. The next example shows a reusable class \s-1FOO\s0 which allows the | |
562 | method \s-1\fIGOOP::BAZ\s0()\fR to be used in place of \s-1\fIFOO::BAZ\s0()\fR. | |
563 | .PP | |
564 | .Vb 1 | |
565 | \& package FOO; | |
566 | .Ve | |
567 | .PP | |
568 | .Vb 8 | |
569 | \& sub new { | |
570 | \& my $type = shift; | |
571 | \& bless {}, $type; | |
572 | \& } | |
573 | \& sub bar { | |
574 | \& my $self = shift; | |
575 | \& $self->BAZ; | |
576 | \& } | |
577 | .Ve | |
578 | .PP | |
579 | .Vb 3 | |
580 | \& sub BAZ { | |
581 | \& print "in BAZ\en"; | |
582 | \& } | |
583 | .Ve | |
584 | .PP | |
585 | .Vb 2 | |
586 | \& package GOOP; | |
587 | \& @ISA = qw( FOO ); | |
588 | .Ve | |
589 | .PP | |
590 | .Vb 7 | |
591 | \& sub new { | |
592 | \& my $type = shift; | |
593 | \& bless {}, $type; | |
594 | \& } | |
595 | \& sub BAZ { | |
596 | \& print "in GOOP::BAZ\en"; | |
597 | \& } | |
598 | .Ve | |
599 | .PP | |
600 | .Vb 1 | |
601 | \& package main; | |
602 | .Ve | |
603 | .PP | |
604 | .Vb 2 | |
605 | \& $a = GOOP->new; | |
606 | \& $a->bar; | |
607 | .Ve | |
608 | .SH "CLASS CONTEXT AND THE OBJECT" | |
609 | .IX Header "CLASS CONTEXT AND THE OBJECT" | |
610 | Use the object to solve package and class context problems. Everything a | |
611 | method needs should be available via the object or should be passed as a | |
612 | parameter to the method. | |
613 | .PP | |
614 | A class will sometimes have static or global data to be used by the | |
615 | methods. A subclass may want to override that data and replace it with new | |
616 | data. When this happens the superclass may not know how to find the new | |
617 | copy of the data. | |
618 | .PP | |
619 | This problem can be solved by using the object to define the context of the | |
620 | method. Let the method look in the object for a reference to the data. The | |
621 | alternative is to force the method to go hunting for the data (\*(L"Is it in my | |
622 | class, or in a subclass? Which subclass?\*(R"), and this can be inconvenient | |
623 | and will lead to hackery. It is better just to let the object tell the | |
624 | method where that data is located. | |
625 | .PP | |
626 | .Vb 1 | |
627 | \& package Bar; | |
628 | .Ve | |
629 | .PP | |
630 | .Vb 1 | |
631 | \& %fizzle = ( 'Password' => 'XYZZY' ); | |
632 | .Ve | |
633 | .PP | |
634 | .Vb 6 | |
635 | \& sub new { | |
636 | \& my $type = shift; | |
637 | \& my $self = {}; | |
638 | \& $self->{'fizzle'} = \e%fizzle; | |
639 | \& bless $self, $type; | |
640 | \& } | |
641 | .Ve | |
642 | .PP | |
643 | .Vb 2 | |
644 | \& sub enter { | |
645 | \& my $self = shift; | |
646 | .Ve | |
647 | .PP | |
648 | .Vb 5 | |
649 | \& # Don't try to guess if we should use %Bar::fizzle | |
650 | \& # or %Foo::fizzle. The object already knows which | |
651 | \& # we should use, so just ask it. | |
652 | \& # | |
653 | \& my $fizzle = $self->{'fizzle'}; | |
654 | .Ve | |
655 | .PP | |
656 | .Vb 2 | |
657 | \& print "The word is ", $fizzle->{'Password'}, "\en"; | |
658 | \& } | |
659 | .Ve | |
660 | .PP | |
661 | .Vb 2 | |
662 | \& package Foo; | |
663 | \& @ISA = qw( Bar ); | |
664 | .Ve | |
665 | .PP | |
666 | .Vb 1 | |
667 | \& %fizzle = ( 'Password' => 'Rumple' ); | |
668 | .Ve | |
669 | .PP | |
670 | .Vb 6 | |
671 | \& sub new { | |
672 | \& my $type = shift; | |
673 | \& my $self = Bar->new; | |
674 | \& $self->{'fizzle'} = \e%fizzle; | |
675 | \& bless $self, $type; | |
676 | \& } | |
677 | .Ve | |
678 | .PP | |
679 | .Vb 1 | |
680 | \& package main; | |
681 | .Ve | |
682 | .PP | |
683 | .Vb 4 | |
684 | \& $a = Bar->new; | |
685 | \& $b = Foo->new; | |
686 | \& $a->enter; | |
687 | \& $b->enter; | |
688 | .Ve | |
689 | .SH "INHERITING A CONSTRUCTOR" | |
690 | .IX Header "INHERITING A CONSTRUCTOR" | |
691 | An inheritable constructor should use the second form of \fIbless()\fR which allows | |
692 | blessing directly into a specified class. Notice in this example that the | |
693 | object will be a \s-1BAR\s0 not a \s-1FOO\s0, even though the constructor is in class \s-1FOO\s0. | |
694 | .PP | |
695 | .Vb 1 | |
696 | \& package FOO; | |
697 | .Ve | |
698 | .PP | |
699 | .Vb 5 | |
700 | \& sub new { | |
701 | \& my $type = shift; | |
702 | \& my $self = {}; | |
703 | \& bless $self, $type; | |
704 | \& } | |
705 | .Ve | |
706 | .PP | |
707 | .Vb 3 | |
708 | \& sub baz { | |
709 | \& print "in FOO::baz()\en"; | |
710 | \& } | |
711 | .Ve | |
712 | .PP | |
713 | .Vb 2 | |
714 | \& package BAR; | |
715 | \& @ISA = qw(FOO); | |
716 | .Ve | |
717 | .PP | |
718 | .Vb 3 | |
719 | \& sub baz { | |
720 | \& print "in BAR::baz()\en"; | |
721 | \& } | |
722 | .Ve | |
723 | .PP | |
724 | .Vb 1 | |
725 | \& package main; | |
726 | .Ve | |
727 | .PP | |
728 | .Vb 2 | |
729 | \& $a = BAR->new; | |
730 | \& $a->baz; | |
731 | .Ve | |
732 | .SH "DELEGATION" | |
733 | .IX Header "DELEGATION" | |
734 | Some classes, such as SDBM_File, cannot be effectively subclassed because | |
735 | they create foreign objects. Such a class can be extended with some sort of | |
736 | aggregation technique such as the \*(L"using\*(R" relationship mentioned earlier or | |
737 | by delegation. | |
738 | .PP | |
739 | The following example demonstrates delegation using an \s-1\fIAUTOLOAD\s0()\fR function to | |
740 | perform message\-forwarding. This will allow the Mydbm object to behave | |
741 | exactly like an SDBM_File object. The Mydbm class could now extend the | |
742 | behavior by adding custom \s-1\fIFETCH\s0()\fR and \s-1\fISTORE\s0()\fR methods, if this is desired. | |
743 | .PP | |
744 | .Vb 1 | |
745 | \& package Mydbm; | |
746 | .Ve | |
747 | .PP | |
748 | .Vb 3 | |
749 | \& require SDBM_File; | |
750 | \& require Tie::Hash; | |
751 | \& @ISA = qw(Tie::Hash); | |
752 | .Ve | |
753 | .PP | |
754 | .Vb 5 | |
755 | \& sub TIEHASH { | |
756 | \& my $type = shift; | |
757 | \& my $ref = SDBM_File->new(@_); | |
758 | \& bless {'delegate' => $ref}; | |
759 | \& } | |
760 | .Ve | |
761 | .PP | |
762 | .Vb 2 | |
763 | \& sub AUTOLOAD { | |
764 | \& my $self = shift; | |
765 | .Ve | |
766 | .PP | |
767 | .Vb 2 | |
768 | \& # The Perl interpreter places the name of the | |
769 | \& # message in a variable called $AUTOLOAD. | |
770 | .Ve | |
771 | .PP | |
772 | .Vb 2 | |
773 | \& # DESTROY messages should never be propagated. | |
774 | \& return if $AUTOLOAD =~ /::DESTROY$/; | |
775 | .Ve | |
776 | .PP | |
777 | .Vb 2 | |
778 | \& # Remove the package name. | |
779 | \& $AUTOLOAD =~ s/^Mydbm:://; | |
780 | .Ve | |
781 | .PP | |
782 | .Vb 3 | |
783 | \& # Pass the message to the delegate. | |
784 | \& $self->{'delegate'}->$AUTOLOAD(@_); | |
785 | \& } | |
786 | .Ve | |
787 | .PP | |
788 | .Vb 2 | |
789 | \& package main; | |
790 | \& use Fcntl qw( O_RDWR O_CREAT ); | |
791 | .Ve | |
792 | .PP | |
793 | .Vb 3 | |
794 | \& tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; | |
795 | \& $foo{'bar'} = 123; | |
796 | \& print "foo-bar = $foo{'bar'}\en"; | |
797 | .Ve |