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