3 perlbot - Bag'o Object Tricks For Perl5 (the BOT)
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 comprehensive guide to Perl5's
12 object oriented features, nor should it be construed as a style guide.
14 The Perl motto still holds: There's more than one way to do it.
16 =head1 INSTANCE VARIABLES
18 An anonymous array or anonymous hash can be used to hold instance
19 variables. Named parameters are also demonstrated.
27 $self->{'High'} = $params{'High'};
28 $self->{'Low'} = $params{'Low'};
39 $self->[0] = $params{'Left'};
40 $self->[1] = $params{'Right'};
46 $a = new Foo ( 'High' => 42, 'Low' => 11 );
47 print "High=$a->{'High'}\n";
48 print "Low=$a->{'Low'}\n";
50 $b = new Bar ( 'Left' => 78, 'Right' => 40 );
51 print "Left=$b->[0]\n";
52 print "Right=$b->[1]\n";
55 =head1 SCALAR INSTANCE VARIABLES
57 An anonymous scalar can be used when only one instance variable is needed.
74 =head1 INSTANCE VARIABLE INHERITANCE
76 This example demonstrates how one might inherit instance variables from a
77 superclass for inclusion in the new class. This requires calling the
78 superclass's constructor and adding one's own instance variables to the new
101 print "buz = ", $a->{'buz'}, "\n";
102 print "biz = ", $a->{'biz'}, "\n";
106 =head1 OBJECT RELATIONSHIPS
108 The following demonstrates how one might implement "containing" and "using"
109 relationships between objects.
123 $self->{'Bar'} = new Bar ();
131 print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
132 print "biz = ", $a->{'biz'}, "\n";
136 =head1 OVERRIDING SUPERCLASS METHODS
138 The following example demonstrates how one might override a superclass
139 method and then call the method after it has been overridden. The
140 Foo::Inherit class allows the programmer to call an overridden superclass
141 method without actually knowing where that method is defined.
145 sub goo { print "here's the goo\n" }
147 package Bar; @ISA = qw( Buz );
148 sub google { print "google here\n" }
151 sub mumble { print "mumbling\n" }
154 @ISA = qw( Bar Baz );
155 @Foo::Inherit::ISA = @ISA; # Access to overridden methods.
158 sub grr { print "grumble\n" }
161 $self->Foo::Inherit::goo();
165 $self->Foo::Inherit::mumble();
169 $self->Foo::Inherit::google();
181 =head1 USING RELATIONSHIP WITH SDBM
183 This example demonstrates an interface for the SDBM class. This creates a
184 "using" relationship between the SDBM class and the new class Mydbm.
193 my $ref = SDBM_File->new(@_);
194 bless {'dbm' => $ref};
198 my $ref = $self->{'dbm'};
204 my $ref = $self->{'dbm'};
207 die "Cannot STORE an undefined key in Mydbm\n";
213 tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
215 print "foo-bar = $foo{'bar'}\n";
217 tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
219 print "bar-Cathy = $bar{'Cathy'}\n";
221 =head1 THINKING OF CODE REUSE
223 One strength of Object-Oriented languages is the ease with which old code
224 can use new code. The following examples will demonstrate first how one can
225 hinder code reuse and then how one can promote code reuse.
227 This first example illustrates a class which uses a fully-qualified method
228 call to access the "private" method BAZ(). The second example will show
229 that it is impossible to override the BAZ() method.
236 $self->FOO::private::BAZ;
239 package FOO::private;
250 Now we try to override the BAZ() method. We would like FOO::bar() to call
251 GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
259 $self->FOO::private::BAZ;
262 package FOO::private;
273 print "in GOOP::BAZ\n";
281 To create reusable code we must modify class FOO, flattening class
282 FOO::private. The next example shows a reusable class FOO which allows the
283 method GOOP::BAZ() to be used in place of FOO::BAZ().
302 print "in GOOP::BAZ\n";
310 =head1 CLASS CONTEXT AND THE OBJECT
312 Use the object to solve package and class context problems. Everything a
313 method needs should be available via the object or should be passed as a
314 parameter to the method.
316 A class will sometimes have static or global data to be used by the
317 methods. A subclass may want to override that data and replace it with new
318 data. When this happens the superclass may not know how to find the new
321 This problem can be solved by using the object to define the context of the
322 method. Let the method look in the object for a reference to the data. The
323 alternative is to force the method to go hunting for the data ("Is it in my
324 class, or in a subclass? Which subclass?"), and this can be inconvenient
325 and will lead to hackery. It is better to just let the object tell the
326 method where that data is located.
330 %fizzle = ( 'Password' => 'XYZZY' );
334 $self->{'fizzle'} = \%fizzle;
341 # Don't try to guess if we should use %Bar::fizzle
342 # or %Foo::fizzle. The object already knows which
343 # we should use, so just ask it.
345 my $fizzle = $self->{'fizzle'};
347 print "The word is ", $fizzle->{'Password'}, "\n";
353 %fizzle = ( 'Password' => 'Rumple' );
357 $self->{'fizzle'} = \%fizzle;
368 =head1 INHERITING A CONSTRUCTOR
370 An inheritable constructor should use the second form of bless() which allows
371 blessing directly into a specified class. Notice in this example that the
372 object will be a BAR not a FOO, even though the constructor is in class FOO.
383 print "in FOO::baz()\n";
390 print "in BAR::baz()\n";
400 Some classes, such as SDBM_File, cannot be effectively subclassed because
401 they create foreign objects. Such a class can be extended with some sort of
402 aggregation technique such as the "using" relationship mentioned earlier or
405 The following example demonstrates delegation using an AUTOLOAD() function to
406 perform message-forwarding. This will allow the Mydbm object to behave
407 exactly like an SDBM_File object. The Mydbm class could now extend the
408 behavior by adding custom FETCH() and STORE() methods, if this is desired.
418 my $ref = SDBM_File->new(@_);
419 bless {'delegate' => $ref};
425 # The Perl interpreter places the name of the
426 # message in a variable called $AUTOLOAD.
428 # DESTROY messages should never be propagated.
429 return if $AUTOLOAD =~ /::DESTROY$/;
431 # Remove the package name.
432 $AUTOLOAD =~ s/^Mydbm:://;
434 # Pass the message to the delegate.
435 $self->{'delegate'}->$AUTOLOAD(@_);
439 use Fcntl qw( O_RDWR O_CREAT );
441 tie %foo, Mydbm, "adbm", O_RDWR|O_CREAT, 0640;
443 print "foo-bar = $foo{'bar'}\n";