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