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