Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | =head1 NAME |
2 | ||
c2960299 | 3 | perlbot - Bag'o Object Tricks (the BOT) |
a0d0e21e | 4 | |
cb1a09d0 | 5 | =head1 DESCRIPTION |
a0d0e21e LW |
6 | |
7 | The following collection of tricks and hints is intended to whet curious | |
8 | appetites about such things as the use of instance variables and the | |
9 | mechanics of object and class relationships. The reader is encouraged to | |
10 | consult relevant textbooks for discussion of Object Oriented definitions and | |
c2960299 AD |
11 | methodology. This is not intended as a tutorial for object-oriented |
12 | programming or as a comprehensive guide to Perl's object oriented features, | |
4073dd47 CW |
13 | nor should it be construed as a style guide. If you're looking for tutorials, |
14 | be sure to read L<perlboot>, L<perltoot>, and L<perltooc>. | |
a0d0e21e LW |
15 | |
16 | The Perl motto still holds: There's more than one way to do it. | |
17 | ||
c2960299 AD |
18 | =head1 OO SCALING TIPS |
19 | ||
20 | =over 5 | |
21 | ||
22 | =item 1 | |
23 | ||
24 | Do not attempt to verify the type of $self. That'll break if the class is | |
25 | inherited, when the type of $self is valid but its package isn't what you | |
26 | expect. See rule 5. | |
27 | ||
28 | =item 2 | |
29 | ||
30 | If an object-oriented (OO) or indirect-object (IO) syntax was used, then the | |
31 | object is probably the correct type and there's no need to become paranoid | |
32 | about it. Perl isn't a paranoid language anyway. If people subvert the OO | |
33 | or IO syntax then they probably know what they're doing and you should let | |
34 | them do it. See rule 1. | |
35 | ||
36 | =item 3 | |
37 | ||
38 | Use the two-argument form of bless(). Let a subclass use your constructor. | |
39 | See L<INHERITING A CONSTRUCTOR>. | |
40 | ||
41 | =item 4 | |
42 | ||
43 | The subclass is allowed to know things about its immediate superclass, the | |
44 | superclass is allowed to know nothing about a subclass. | |
45 | ||
46 | =item 5 | |
47 | ||
48 | Don't be trigger happy with inheritance. A "using", "containing", or | |
49 | "delegation" relationship (some sort of aggregation, at least) is often more | |
50 | appropriate. See L<OBJECT RELATIONSHIPS>, L<USING RELATIONSHIP WITH SDBM>, | |
51 | and L<"DELEGATION">. | |
52 | ||
53 | =item 6 | |
54 | ||
55 | The object is the namespace. Make package globals accessible via the | |
56 | object. This will remove the guess work about the symbol's home package. | |
57 | See L<CLASS CONTEXT AND THE OBJECT>. | |
58 | ||
59 | =item 7 | |
60 | ||
5f05dabc | 61 | IO syntax is certainly less noisy, but it is also prone to ambiguities that |
c2960299 AD |
62 | can cause difficult-to-find bugs. Allow people to use the sure-thing OO |
63 | syntax, even if you don't like it. | |
64 | ||
65 | =item 8 | |
66 | ||
67 | Do not use function-call syntax on a method. You're going to be bitten | |
68 | someday. Someone might move that method into a superclass and your code | |
69 | will be broken. On top of that you're feeding the paranoia in rule 2. | |
70 | ||
71 | =item 9 | |
72 | ||
73 | Don't assume you know the home package of a method. You're making it | |
74 | difficult for someone to override that method. See L<THINKING OF CODE REUSE>. | |
75 | ||
76 | =back | |
77 | ||
a0d0e21e LW |
78 | =head1 INSTANCE VARIABLES |
79 | ||
80 | An anonymous array or anonymous hash can be used to hold instance | |
81 | variables. Named parameters are also demonstrated. | |
82 | ||
83 | package Foo; | |
84 | ||
85 | sub new { | |
84f709e7 | 86 | my $type = shift; |
a0d0e21e | 87 | my %params = @_; |
84f709e7 JH |
88 | my $self = {}; |
89 | $self->{'High'} = $params{'High'}; | |
90 | $self->{'Low'} = $params{'Low'}; | |
c2960299 | 91 | bless $self, $type; |
a0d0e21e LW |
92 | } |
93 | ||
94 | ||
95 | package Bar; | |
96 | ||
97 | sub new { | |
84f709e7 | 98 | my $type = shift; |
a0d0e21e | 99 | my %params = @_; |
84f709e7 JH |
100 | my $self = []; |
101 | $self->[0] = $params{'Left'}; | |
102 | $self->[1] = $params{'Right'}; | |
c2960299 | 103 | bless $self, $type; |
a0d0e21e LW |
104 | } |
105 | ||
106 | package main; | |
107 | ||
84f709e7 JH |
108 | $a = Foo->new( 'High' => 42, 'Low' => 11 ); |
109 | print "High=$a->{'High'}\n"; | |
110 | print "Low=$a->{'Low'}\n"; | |
a0d0e21e | 111 | |
84f709e7 JH |
112 | $b = Bar->new( 'Left' => 78, 'Right' => 40 ); |
113 | print "Left=$b->[0]\n"; | |
114 | print "Right=$b->[1]\n"; | |
a0d0e21e | 115 | |
a0d0e21e LW |
116 | =head1 SCALAR INSTANCE VARIABLES |
117 | ||
118 | An anonymous scalar can be used when only one instance variable is needed. | |
119 | ||
120 | package Foo; | |
121 | ||
122 | sub new { | |
123 | my $type = shift; | |
84f709e7 JH |
124 | my $self; |
125 | $self = shift; | |
c2960299 | 126 | bless \$self, $type; |
a0d0e21e LW |
127 | } |
128 | ||
129 | package main; | |
130 | ||
84f709e7 JH |
131 | $a = Foo->new( 42 ); |
132 | print "a=$$a\n"; | |
a0d0e21e LW |
133 | |
134 | ||
135 | =head1 INSTANCE VARIABLE INHERITANCE | |
136 | ||
137 | This example demonstrates how one might inherit instance variables from a | |
138 | superclass for inclusion in the new class. This requires calling the | |
139 | superclass's constructor and adding one's own instance variables to the new | |
140 | object. | |
141 | ||
142 | package Bar; | |
143 | ||
144 | sub new { | |
c2960299 | 145 | my $type = shift; |
a0d0e21e | 146 | my $self = {}; |
84f709e7 | 147 | $self->{'buz'} = 42; |
c2960299 | 148 | bless $self, $type; |
a0d0e21e LW |
149 | } |
150 | ||
151 | package Foo; | |
84f709e7 | 152 | @ISA = qw( Bar ); |
a0d0e21e LW |
153 | |
154 | sub new { | |
c2960299 AD |
155 | my $type = shift; |
156 | my $self = Bar->new; | |
84f709e7 | 157 | $self->{'biz'} = 11; |
c2960299 | 158 | bless $self, $type; |
a0d0e21e LW |
159 | } |
160 | ||
161 | package main; | |
162 | ||
84f709e7 JH |
163 | $a = Foo->new; |
164 | print "buz = ", $a->{'buz'}, "\n"; | |
165 | print "biz = ", $a->{'biz'}, "\n"; | |
a0d0e21e LW |
166 | |
167 | ||
168 | ||
169 | =head1 OBJECT RELATIONSHIPS | |
170 | ||
171 | The following demonstrates how one might implement "containing" and "using" | |
172 | relationships between objects. | |
173 | ||
174 | package Bar; | |
175 | ||
176 | sub new { | |
c2960299 | 177 | my $type = shift; |
a0d0e21e | 178 | my $self = {}; |
84f709e7 | 179 | $self->{'buz'} = 42; |
c2960299 | 180 | bless $self, $type; |
a0d0e21e LW |
181 | } |
182 | ||
183 | package Foo; | |
184 | ||
185 | sub new { | |
c2960299 | 186 | my $type = shift; |
a0d0e21e | 187 | my $self = {}; |
84f709e7 JH |
188 | $self->{'Bar'} = Bar->new; |
189 | $self->{'biz'} = 11; | |
c2960299 | 190 | bless $self, $type; |
a0d0e21e LW |
191 | } |
192 | ||
193 | package main; | |
194 | ||
84f709e7 JH |
195 | $a = Foo->new; |
196 | print "buz = ", $a->{'Bar'}->{'buz'}, "\n"; | |
197 | print "biz = ", $a->{'biz'}, "\n"; | |
a0d0e21e LW |
198 | |
199 | ||
200 | ||
201 | =head1 OVERRIDING SUPERCLASS METHODS | |
202 | ||
4633a7c4 LW |
203 | The following example demonstrates how to override a superclass method and |
204 | then call the overridden method. The B<SUPER> pseudo-class allows the | |
205 | programmer to call an overridden superclass method without actually knowing | |
206 | where that method is defined. | |
a0d0e21e LW |
207 | |
208 | package Buz; | |
209 | sub goo { print "here's the goo\n" } | |
210 | ||
84f709e7 | 211 | package Bar; @ISA = qw( Buz ); |
a0d0e21e LW |
212 | sub google { print "google here\n" } |
213 | ||
214 | package Baz; | |
215 | sub mumble { print "mumbling\n" } | |
216 | ||
217 | package Foo; | |
84f709e7 | 218 | @ISA = qw( Bar Baz ); |
a0d0e21e | 219 | |
c2960299 AD |
220 | sub new { |
221 | my $type = shift; | |
222 | bless [], $type; | |
223 | } | |
a0d0e21e LW |
224 | sub grr { print "grumble\n" } |
225 | sub goo { | |
226 | my $self = shift; | |
4633a7c4 | 227 | $self->SUPER::goo(); |
a0d0e21e LW |
228 | } |
229 | sub mumble { | |
230 | my $self = shift; | |
4633a7c4 | 231 | $self->SUPER::mumble(); |
a0d0e21e LW |
232 | } |
233 | sub google { | |
234 | my $self = shift; | |
4633a7c4 | 235 | $self->SUPER::google(); |
a0d0e21e LW |
236 | } |
237 | ||
238 | package main; | |
239 | ||
84f709e7 | 240 | $foo = Foo->new; |
a0d0e21e LW |
241 | $foo->mumble; |
242 | $foo->grr; | |
243 | $foo->goo; | |
244 | $foo->google; | |
245 | ||
246 | ||
c2960299 | 247 | =head1 USING RELATIONSHIP WITH SDBM |
a0d0e21e LW |
248 | |
249 | This example demonstrates an interface for the SDBM class. This creates a | |
250 | "using" relationship between the SDBM class and the new class Mydbm. | |
251 | ||
a0d0e21e LW |
252 | package Mydbm; |
253 | ||
84f709e7 JH |
254 | require SDBM_File; |
255 | require Tie::Hash; | |
256 | @ISA = qw( Tie::Hash ); | |
c2960299 | 257 | |
a0d0e21e | 258 | sub TIEHASH { |
c2960299 | 259 | my $type = shift; |
a0d0e21e | 260 | my $ref = SDBM_File->new(@_); |
84f709e7 | 261 | bless {'dbm' => $ref}, $type; |
a0d0e21e LW |
262 | } |
263 | sub FETCH { | |
264 | my $self = shift; | |
84f709e7 | 265 | my $ref = $self->{'dbm'}; |
a0d0e21e LW |
266 | $ref->FETCH(@_); |
267 | } | |
268 | sub STORE { | |
54310121 | 269 | my $self = shift; |
84f709e7 JH |
270 | if (defined $_[0]){ |
271 | my $ref = $self->{'dbm'}; | |
a0d0e21e LW |
272 | $ref->STORE(@_); |
273 | } else { | |
274 | die "Cannot STORE an undefined key in Mydbm\n"; | |
275 | } | |
276 | } | |
277 | ||
278 | package main; | |
c2960299 | 279 | use Fcntl qw( O_RDWR O_CREAT ); |
a0d0e21e | 280 | |
84f709e7 JH |
281 | tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640; |
282 | $foo{'bar'} = 123; | |
283 | print "foo-bar = $foo{'bar'}\n"; | |
a0d0e21e | 284 | |
84f709e7 JH |
285 | tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640; |
286 | $bar{'Cathy'} = 456; | |
287 | print "bar-Cathy = $bar{'Cathy'}\n"; | |
a0d0e21e LW |
288 | |
289 | =head1 THINKING OF CODE REUSE | |
290 | ||
291 | One strength of Object-Oriented languages is the ease with which old code | |
292 | can use new code. The following examples will demonstrate first how one can | |
293 | hinder code reuse and then how one can promote code reuse. | |
294 | ||
295 | This first example illustrates a class which uses a fully-qualified method | |
296 | call to access the "private" method BAZ(). The second example will show | |
297 | that it is impossible to override the BAZ() method. | |
298 | ||
299 | package FOO; | |
300 | ||
c2960299 AD |
301 | sub new { |
302 | my $type = shift; | |
303 | bless {}, $type; | |
304 | } | |
a0d0e21e LW |
305 | sub bar { |
306 | my $self = shift; | |
307 | $self->FOO::private::BAZ; | |
308 | } | |
309 | ||
310 | package FOO::private; | |
311 | ||
312 | sub BAZ { | |
313 | print "in BAZ\n"; | |
314 | } | |
315 | ||
316 | package main; | |
317 | ||
84f709e7 | 318 | $a = FOO->new; |
a0d0e21e LW |
319 | $a->bar; |
320 | ||
321 | Now we try to override the BAZ() method. We would like FOO::bar() to call | |
d1b91892 | 322 | GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls |
a0d0e21e LW |
323 | FOO::private::BAZ(). |
324 | ||
325 | package FOO; | |
326 | ||
c2960299 AD |
327 | sub new { |
328 | my $type = shift; | |
329 | bless {}, $type; | |
330 | } | |
a0d0e21e LW |
331 | sub bar { |
332 | my $self = shift; | |
333 | $self->FOO::private::BAZ; | |
334 | } | |
335 | ||
336 | package FOO::private; | |
337 | ||
338 | sub BAZ { | |
339 | print "in BAZ\n"; | |
340 | } | |
341 | ||
342 | package GOOP; | |
84f709e7 | 343 | @ISA = qw( FOO ); |
c2960299 AD |
344 | sub new { |
345 | my $type = shift; | |
346 | bless {}, $type; | |
347 | } | |
a0d0e21e LW |
348 | |
349 | sub BAZ { | |
350 | print "in GOOP::BAZ\n"; | |
351 | } | |
352 | ||
353 | package main; | |
354 | ||
84f709e7 | 355 | $a = GOOP->new; |
a0d0e21e LW |
356 | $a->bar; |
357 | ||
358 | To create reusable code we must modify class FOO, flattening class | |
359 | FOO::private. The next example shows a reusable class FOO which allows the | |
360 | method GOOP::BAZ() to be used in place of FOO::BAZ(). | |
361 | ||
362 | package FOO; | |
363 | ||
c2960299 AD |
364 | sub new { |
365 | my $type = shift; | |
366 | bless {}, $type; | |
367 | } | |
a0d0e21e LW |
368 | sub bar { |
369 | my $self = shift; | |
370 | $self->BAZ; | |
371 | } | |
372 | ||
373 | sub BAZ { | |
374 | print "in BAZ\n"; | |
375 | } | |
376 | ||
377 | package GOOP; | |
84f709e7 | 378 | @ISA = qw( FOO ); |
a0d0e21e | 379 | |
c2960299 AD |
380 | sub new { |
381 | my $type = shift; | |
382 | bless {}, $type; | |
383 | } | |
a0d0e21e LW |
384 | sub BAZ { |
385 | print "in GOOP::BAZ\n"; | |
386 | } | |
387 | ||
388 | package main; | |
389 | ||
84f709e7 | 390 | $a = GOOP->new; |
a0d0e21e LW |
391 | $a->bar; |
392 | ||
393 | =head1 CLASS CONTEXT AND THE OBJECT | |
394 | ||
395 | Use the object to solve package and class context problems. Everything a | |
396 | method needs should be available via the object or should be passed as a | |
397 | parameter to the method. | |
398 | ||
399 | A class will sometimes have static or global data to be used by the | |
400 | methods. A subclass may want to override that data and replace it with new | |
401 | data. When this happens the superclass may not know how to find the new | |
402 | copy of the data. | |
403 | ||
404 | This problem can be solved by using the object to define the context of the | |
405 | method. Let the method look in the object for a reference to the data. The | |
406 | alternative is to force the method to go hunting for the data ("Is it in my | |
407 | class, or in a subclass? Which subclass?"), and this can be inconvenient | |
5f05dabc | 408 | and will lead to hackery. It is better just to let the object tell the |
a0d0e21e LW |
409 | method where that data is located. |
410 | ||
411 | package Bar; | |
412 | ||
84f709e7 | 413 | %fizzle = ( 'Password' => 'XYZZY' ); |
a0d0e21e LW |
414 | |
415 | sub new { | |
c2960299 | 416 | my $type = shift; |
a0d0e21e | 417 | my $self = {}; |
84f709e7 | 418 | $self->{'fizzle'} = \%fizzle; |
c2960299 | 419 | bless $self, $type; |
a0d0e21e LW |
420 | } |
421 | ||
422 | sub enter { | |
423 | my $self = shift; | |
54310121 | 424 | |
a0d0e21e LW |
425 | # Don't try to guess if we should use %Bar::fizzle |
426 | # or %Foo::fizzle. The object already knows which | |
427 | # we should use, so just ask it. | |
428 | # | |
84f709e7 | 429 | my $fizzle = $self->{'fizzle'}; |
a0d0e21e | 430 | |
84f709e7 | 431 | print "The word is ", $fizzle->{'Password'}, "\n"; |
a0d0e21e LW |
432 | } |
433 | ||
434 | package Foo; | |
84f709e7 | 435 | @ISA = qw( Bar ); |
a0d0e21e | 436 | |
84f709e7 | 437 | %fizzle = ( 'Password' => 'Rumple' ); |
a0d0e21e LW |
438 | |
439 | sub new { | |
c2960299 | 440 | my $type = shift; |
a0d0e21e | 441 | my $self = Bar->new; |
84f709e7 | 442 | $self->{'fizzle'} = \%fizzle; |
c2960299 | 443 | bless $self, $type; |
a0d0e21e LW |
444 | } |
445 | ||
446 | package main; | |
447 | ||
84f709e7 JH |
448 | $a = Bar->new; |
449 | $b = Foo->new; | |
a0d0e21e LW |
450 | $a->enter; |
451 | $b->enter; | |
452 | ||
d1b91892 AD |
453 | =head1 INHERITING A CONSTRUCTOR |
454 | ||
455 | An inheritable constructor should use the second form of bless() which allows | |
456 | blessing directly into a specified class. Notice in this example that the | |
457 | object will be a BAR not a FOO, even though the constructor is in class FOO. | |
458 | ||
459 | package FOO; | |
460 | ||
461 | sub new { | |
462 | my $type = shift; | |
463 | my $self = {}; | |
464 | bless $self, $type; | |
465 | } | |
466 | ||
467 | sub baz { | |
468 | print "in FOO::baz()\n"; | |
469 | } | |
470 | ||
471 | package BAR; | |
84f709e7 | 472 | @ISA = qw(FOO); |
d1b91892 AD |
473 | |
474 | sub baz { | |
475 | print "in BAR::baz()\n"; | |
476 | } | |
477 | ||
478 | package main; | |
479 | ||
84f709e7 | 480 | $a = BAR->new; |
d1b91892 AD |
481 | $a->baz; |
482 | ||
483 | =head1 DELEGATION | |
484 | ||
485 | Some classes, such as SDBM_File, cannot be effectively subclassed because | |
486 | they create foreign objects. Such a class can be extended with some sort of | |
487 | aggregation technique such as the "using" relationship mentioned earlier or | |
488 | by delegation. | |
489 | ||
490 | The following example demonstrates delegation using an AUTOLOAD() function to | |
491 | perform message-forwarding. This will allow the Mydbm object to behave | |
492 | exactly like an SDBM_File object. The Mydbm class could now extend the | |
493 | behavior by adding custom FETCH() and STORE() methods, if this is desired. | |
494 | ||
495 | package Mydbm; | |
496 | ||
84f709e7 JH |
497 | require SDBM_File; |
498 | require Tie::Hash; | |
499 | @ISA = qw(Tie::Hash); | |
d1b91892 AD |
500 | |
501 | sub TIEHASH { | |
502 | my $type = shift; | |
84f709e7 JH |
503 | my $ref = SDBM_File->new(@_); |
504 | bless {'delegate' => $ref}; | |
d1b91892 AD |
505 | } |
506 | ||
507 | sub AUTOLOAD { | |
508 | my $self = shift; | |
509 | ||
510 | # The Perl interpreter places the name of the | |
511 | # message in a variable called $AUTOLOAD. | |
512 | ||
513 | # DESTROY messages should never be propagated. | |
514 | return if $AUTOLOAD =~ /::DESTROY$/; | |
515 | ||
516 | # Remove the package name. | |
517 | $AUTOLOAD =~ s/^Mydbm:://; | |
518 | ||
519 | # Pass the message to the delegate. | |
84f709e7 | 520 | $self->{'delegate'}->$AUTOLOAD(@_); |
d1b91892 AD |
521 | } |
522 | ||
523 | package main; | |
524 | use Fcntl qw( O_RDWR O_CREAT ); | |
525 | ||
84f709e7 JH |
526 | tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640; |
527 | $foo{'bar'} = 123; | |
528 | print "foo-bar = $foo{'bar'}\n"; | |
4073dd47 CW |
529 | |
530 | =head1 SEE ALSO | |
531 | ||
532 | L<perlboot>, L<perltoot>, L<perltooc>. |