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">
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:" />
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"> perlbot - Bag'o Object Tricks
</span></strong></big>
17 <p><a name=
"__index__"></a></p>
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>
42 <h1><a name=
"name">NAME
</a></h1>
43 <p>perlbot - Bag'o Object Tricks (the BOT)
</p>
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>
60 <h1><a name=
"oo_scaling_tips">OO SCALING TIPS
</a></h1>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
121 $self-
>{'High'} = $params{'High'};
122 $self-
>{'Low'} = $params{'Low'};
132 $self-
>[
0] = $params{'Left'};
133 $self-
>[
1] = $params{'Right'};
139 $a = Foo-
>new( 'High' =
> 42, 'Low' =
> 11 );
140 print
"High=$a-
>{'High'}\n
";
141 print
"Low=$a-
>{'Low'}\n
";
</pre>
143 $b = Bar-
>new( 'Left' =
> 78, 'Right' =
> 40 );
144 print
"Left=$b-
>[
0]\n
";
145 print
"Right=$b-
>[
1]\n
";
</pre>
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>
163 $a = Foo-
>new(
42 );
164 print
"a=$$a\n
";
</pre>
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
179 $self-
>{'buz'} =
42;
184 @ISA = qw( Bar );
</pre>
188 my $self = Bar-
>new;
189 $self-
>{'biz'} =
11;
196 print
"buz =
", $a-
>{'buz'},
"\n
";
197 print
"biz =
", $a-
>{'biz'},
"\n
";
</pre>
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>
210 $self-
>{'buz'} =
42;
219 $self-
>{'Bar'} = Bar-
>new;
220 $self-
>{'biz'} =
11;
227 print
"buz =
", $a-
>{'Bar'}-
>{'buz'},
"\n
";
228 print
"biz =
", $a-
>{'biz'},
"\n
";
</pre>
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>
239 sub goo { print
"here's the goo\n
" }
</pre>
241 package Bar; @ISA = qw( Buz );
242 sub google { print
"google here\n
" }
</pre>
245 sub mumble { print
"mumbling\n
" }
</pre>
248 @ISA = qw( Bar Baz );
</pre>
254 sub grr { print
"grumble\n
" }
257 $self-
>SUPER::goo();
261 $self-
>SUPER::mumble();
265 $self-
>SUPER::google();
274 $foo-
>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>
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>
288 @ISA = qw( Tie::Hash );
</pre>
292 my $ref = SDBM_File-
>new(@_);
293 bless {'dbm' =
> $ref}, $type;
297 my $ref = $self-
>{'dbm'};
303 my $ref = $self-
>{'dbm'};
306 die
"Cannot STORE an undefined key in Mydbm\n
";
311 use Fcntl qw( O_RDWR O_CREAT );
</pre>
313 tie %foo,
"Mydbm
",
"Sdbm
", O_RDWR|O_CREAT,
0640;
315 print
"foo-bar = $foo{'bar'}\n
";
</pre>
317 tie %bar,
"Mydbm
",
"Sdbm2
", O_RDWR|O_CREAT,
0640;
319 print
"bar-Cathy = $bar{'Cathy'}\n
";
</pre>
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>
339 $self-
>FOO::private::BAZ;
342 package FOO::private;
</pre>
345 print
"in BAZ\n
";
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>
364 $self-
>FOO::private::BAZ;
367 package FOO::private;
</pre>
370 print
"in BAZ\n
";
381 print
"in GOOP::BAZ\n
";
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>
404 print
"in BAZ\n
";
408 @ISA = qw( FOO );
</pre>
415 print
"in GOOP::BAZ\n
";
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>
442 %fizzle = ( 'Password' =
> 'XYZZY' );
</pre>
447 $self-
>{'fizzle'} = \%fizzle;
452 my $self = shift;
</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-
>{'fizzle'};
</pre>
460 print
"The word is
", $fizzle-
>{'Password'},
"\n
";
464 @ISA = qw( Bar );
</pre>
466 %fizzle = ( 'Password' =
> 'Rumple' );
</pre>
470 my $self = Bar-
>new;
471 $self-
>{'fizzle'} = \%fizzle;
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>
498 print
"in FOO::baz()\n
";
502 @ISA = qw(FOO);
</pre>
505 print
"in BAR::baz()\n
";
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
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>
529 @ISA = qw(Tie::Hash);
</pre>
533 my $ref = SDBM_File-
>new(@_);
534 bless {'delegate' =
> $ref};
538 my $self = shift;
</pre>
540 # The Perl interpreter places the name of the
541 # message in a variable called $AUTOLOAD.
</pre>
543 # DESTROY messages should never be propagated.
544 return if $AUTOLOAD =~ /::DESTROY$/;
</pre>
546 # Remove the package name.
547 $AUTOLOAD =~ s/^Mydbm:://;
</pre>
549 # Pass the message to the delegate.
550 $self-
>{'delegate'}-
>$AUTOLOAD(@_);
554 use Fcntl qw( O_RDWR O_CREAT );
</pre>
556 tie %foo,
"Mydbm
",
"adbm
", O_RDWR|O_CREAT,
0640;
558 print
"foo-bar = $foo{'bar'}\n
";
</pre>
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"> perlbot - Bag'o Object Tricks
</span></strong></big>