home *** CD-ROM | disk | FTP | other *** search
- <!-- $RCSfile$$Revision$$Date$ -->
- <!-- $Log$ -->
- <HTML>
- <TITLE> PERLBOT </TITLE>
- <h2>NAME</h2>
- perlbot - Bag'o Object Tricks For Perl5 (the BOT)
- <p><h2>INTRODUCTION</h2>
- The following collection of tricks and hints is intended to whet curious
- appetites about such things as the use of instance variables and the
- mechanics of object and class relationships. The reader is encouraged to
- consult relevant textbooks for discussion of Object Oriented definitions and
- methodology. This is not intended as a comprehensive guide to Perl5's
- object oriented features, nor should it be construed as a style guide.
- <p>The Perl motto still holds: There's more than one way to do it.
- <p><h2>INSTANCE VARIABLES</h2>
- An anonymous array or anonymous hash can be used to hold instance
- variables. Named parameters are also demonstrated.
- <p><pre>
- package Foo;
- </pre>
- <pre>
- sub new {
- my $type = shift;
- my %params = @_;
- my $self = {};
- $self->{'High'} = $params{'High'};
- $self->{'Low'} = $params{'Low'};
- bless $self;
- }
- </pre>
- <pre>
- package Bar;
- </pre>
- <pre>
- sub new {
- my $type = shift;
- my %params = @_;
- my $self = [];
- $self->[0] = $params{'Left'};
- $self->[1] = $params{'Right'};
- bless $self;
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = new Foo ( 'High' => 42, 'Low' => 11 );
- print "High=$a->{'High'}\n";
- print "Low=$a->{'Low'}\n";
- </pre>
- <pre>
- $b = new Bar ( 'Left' => 78, 'Right' => 40 );
- print "Left=$b->[0]\n";
- print "Right=$b->[1]\n";
- </pre>
- <h2>SCALAR INSTANCE VARIABLES</h2>
- An anonymous scalar can be used when only one instance variable is needed.
- <p><pre>
- package Foo;
- </pre>
- <pre>
- sub new {
- my $type = shift;
- my $self;
- $self = shift;
- bless \$self;
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = new Foo 42;
- print "a=$$a\n";
- </pre>
- <h2>INSTANCE VARIABLE INHERITANCE</h2>
- This example demonstrates how one might inherit instance variables from a
- superclass for inclusion in the new class. This requires calling the
- superclass's constructor and adding one's own instance variables to the new
- object.
- <p><pre>
- package Bar;
- </pre>
- <pre>
- sub new {
- my $self = {};
- $self->{'buz'} = 42;
- bless $self;
- }
- </pre>
- <pre>
- package Foo;
- @ISA = qw( Bar );
- </pre>
- <pre>
- sub new {
- my $self = new Bar;
- $self->{'biz'} = 11;
- bless $self;
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = new Foo;
- print "buz = ", $a->{'buz'}, "\n";
- print "biz = ", $a->{'biz'}, "\n";
- </pre>
- <h2>OBJECT RELATIONSHIPS</h2>
- The following demonstrates how one might implement "containing" and "using"
- relationships between objects.
- <p><pre>
- package Bar;
- </pre>
- <pre>
- sub new {
- my $self = {};
- $self->{'buz'} = 42;
- bless $self;
- }
- </pre>
- <pre>
- package Foo;
- </pre>
- <pre>
- sub new {
- my $self = {};
- $self->{'Bar'} = new Bar ();
- $self->{'biz'} = 11;
- bless $self;
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = new Foo;
- print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
- print "biz = ", $a->{'biz'}, "\n";
- </pre>
- <h2>OVERRIDING SUPERCLASS METHODS</h2>
- The following example demonstrates how one might override a superclass
- method and then call the method after it has been overridden. The
- Foo::Inherit class allows the programmer to call an overridden superclass
- method without actually knowing where that method is defined.
- <p><pre>
- package Buz;
- sub goo { print "here's the goo\n" }
- </pre>
- <pre>
- package Bar; @ISA = qw( Buz );
- sub google { print "google here\n" }
- </pre>
- <pre>
- package Baz;
- sub mumble { print "mumbling\n" }
- </pre>
- <pre>
- package Foo;
- @ISA = qw( Bar Baz );
- @Foo::Inherit::ISA = @ISA; # Access to overridden methods.
- </pre>
- <pre>
- sub new { bless [] }
- sub grr { print "grumble\n" }
- sub goo {
- my $self = shift;
- $self->Foo::Inherit::goo();
- }
- sub mumble {
- my $self = shift;
- $self->Foo::Inherit::mumble();
- }
- sub google {
- my $self = shift;
- $self->Foo::Inherit::google();
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $foo = new Foo;
- $foo->mumble;
- $foo->grr;
- $foo->goo;
- $foo->google;
- </pre>
- <h2>USING RELATIONSHIP WITH SDBM </h2>
- This example demonstrates an interface for the SDBM class. This creates a
- "using" relationship between the SDBM class and the new class Mydbm.
- <p><pre>
- use SDBM_File;
- use POSIX;
- </pre>
- <pre>
- package Mydbm;
- </pre>
- <pre>
- sub TIEHASH {
- my $self = shift;
- my $ref = SDBM_File->new(@_);
- bless {'dbm' => $ref};
- }
- sub FETCH {
- my $self = shift;
- my $ref = $self->{'dbm'};
- $ref->FETCH(@_);
- }
- sub STORE {
- my $self = shift;
- if (defined $_[0]){
- my $ref = $self->{'dbm'};
- $ref->STORE(@_);
- } else {
- die "Cannot STORE an undefined key in Mydbm\n";
- }
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
- $foo{'bar'} = 123;
- print "foo-bar = $foo{'bar'}\n";
- </pre>
- <pre>
- tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
- $bar{'Cathy'} = 456;
- print "bar-Cathy = $bar{'Cathy'}\n";
- </pre>
- <h2>THINKING OF CODE REUSE</h2>
- One strength of Object-Oriented languages is the ease with which old code
- can use new code. The following examples will demonstrate first how one can
- hinder code reuse and then how one can promote code reuse.
- <p>This first example illustrates a class which uses a fully-qualified method
- call to access the "private" method BAZ(). The second example will show
- that it is impossible to override the BAZ() method.
- <p><pre>
- package FOO;
- </pre>
- <pre>
- sub new { bless {} }
- sub bar {
- my $self = shift;
- $self->FOO::private::BAZ;
- }
- </pre>
- <pre>
- package FOO::private;
- </pre>
- <pre>
- sub BAZ {
- print "in BAZ\n";
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = FOO->new;
- $a->bar;
- </pre>
- Now we try to override the BAZ() method. We would like FOO::bar() to call
- GOOP::BAZ(), but this cannot happen since FOO::bar() explicitly calls
- FOO::private::BAZ().
- <p><pre>
- package FOO;
- </pre>
- <pre>
- sub new { bless {} }
- sub bar {
- my $self = shift;
- $self->FOO::private::BAZ;
- }
- </pre>
- <pre>
- package FOO::private;
- </pre>
- <pre>
- sub BAZ {
- print "in BAZ\n";
- }
- </pre>
- <pre>
- package GOOP;
- @ISA = qw( FOO );
- sub new { bless {} }
- </pre>
- <pre>
- sub BAZ {
- print "in GOOP::BAZ\n";
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = GOOP->new;
- $a->bar;
- </pre>
- To create reusable code we must modify class FOO, flattening class
- FOO::private. The next example shows a reusable class FOO which allows the
- method GOOP::BAZ() to be used in place of FOO::BAZ().
- <p><pre>
- package FOO;
- </pre>
- <pre>
- sub new { bless {} }
- sub bar {
- my $self = shift;
- $self->BAZ;
- }
- </pre>
- <pre>
- sub BAZ {
- print "in BAZ\n";
- }
- </pre>
- <pre>
- package GOOP;
- @ISA = qw( FOO );
- </pre>
- <pre>
- sub new { bless {} }
- sub BAZ {
- print "in GOOP::BAZ\n";
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = GOOP->new;
- $a->bar;
- </pre>
- <h2>CLASS CONTEXT AND THE OBJECT</h2>
- Use the object to solve package and class context problems. Everything a
- method needs should be available via the object or should be passed as a
- parameter to the method.
- <p>A class will sometimes have static or global data to be used by the
- methods. A subclass may want to override that data and replace it with new
- data. When this happens the superclass may not know how to find the new
- copy of the data.
- <p>This problem can be solved by using the object to define the context of the
- method. Let the method look in the object for a reference to the data. The
- alternative is to force the method to go hunting for the data ("Is it in my
- class, or in a subclass? Which subclass?"), and this can be inconvenient
- and will lead to hackery. It is better to just let the object tell the
- method where that data is located.
- <p><pre>
- package Bar;
- </pre>
- <pre>
- %fizzle = ( 'Password' => 'XYZZY' );
- </pre>
- <pre>
- sub new {
- my $self = {};
- $self->{'fizzle'} = \%fizzle;
- bless $self;
- }
- </pre>
- <pre>
- sub enter {
- my $self = shift;
-
- # Don't try to guess if we should use %Bar::fizzle
- # or %Foo::fizzle. The object already knows which
- # we should use, so just ask it.
- #
- my $fizzle = $self->{'fizzle'};
- </pre>
-
- <listing>
- print "The word is ", $fizzle->{'Password'}, "\n";
- }
- </listing>
- <pre>
- package Foo;
- @ISA = qw( Bar );
- </pre>
- <pre>
- %fizzle = ( 'Password' => 'Rumple' );
- </pre>
- <pre>
- sub new {
- my $self = Bar->new;
- $self->{'fizzle'} = \%fizzle;
- bless $self;
- }
- </pre>
- <pre>
- package main;
- </pre>
- <pre>
- $a = Bar->new;
- $b = Foo->new;
- $a->enter;
- $b->enter;
- </pre>
-