home *** CD-ROM | disk | FTP | other *** search
- <HTML>
- <HEAD>
- <TITLE>perlbot - Bag'o Object Tricks</TITLE>
- <LINK REL="stylesheet" HREF="../../Active.css" TYPE="text/css">
- <LINK REV="made" HREF="mailto:">
- </HEAD>
-
- <BODY>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> perlbot - Bag'o Object Tricks</P></STRONG>
- </TD></TR>
- </TABLE>
-
- <A NAME="__index__"></A>
- <!-- INDEX BEGIN -->
-
- <UL>
-
- <LI><A HREF="#name">NAME</A></LI>
- <LI><A HREF="#description">DESCRIPTION</A></LI>
- <LI><A HREF="#oo scaling tips">OO SCALING TIPS</A></LI>
- <LI><A HREF="#instance variables">INSTANCE VARIABLES</A></LI>
- <LI><A HREF="#scalar instance variables">SCALAR INSTANCE VARIABLES</A></LI>
- <LI><A HREF="#instance variable inheritance">INSTANCE VARIABLE INHERITANCE</A></LI>
- <LI><A HREF="#object relationships">OBJECT RELATIONSHIPS</A></LI>
- <LI><A HREF="#overriding superclass methods">OVERRIDING SUPERCLASS METHODS</A></LI>
- <LI><A HREF="#using relationship with sdbm">USING RELATIONSHIP WITH SDBM</A></LI>
- <LI><A HREF="#thinking of code reuse">THINKING OF CODE REUSE</A></LI>
- <LI><A HREF="#class context and the object">CLASS CONTEXT AND THE OBJECT</A></LI>
- <LI><A HREF="#inheriting a constructor">INHERITING A CONSTRUCTOR</A></LI>
- <LI><A HREF="#delegation">DELEGATION</A></LI>
- </UL>
- <!-- INDEX END -->
-
- <HR>
- <P>
- <H1><A NAME="name">NAME</A></H1>
- <P>perlbot - Bag'o Object Tricks (the BOT)</P>
- <P>
- <HR>
- <H1><A NAME="description">DESCRIPTION</A></H1>
- <P>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 tutorial for object-oriented
- programming or as a comprehensive guide to Perl's object oriented features,
- nor should it be construed as a style guide.</P>
- <P>The Perl motto still holds: There's more than one way to do it.</P>
- <P>
- <HR>
- <H1><A NAME="oo scaling tips">OO SCALING TIPS</A></H1>
- <OL>
- <LI>
- Do not attempt to verify the type of $self. That'll break if the class is
- inherited, when the type of $self is valid but its package isn't what you
- expect. See rule 5.
- <P></P>
- <LI>
- If an object-oriented (OO) or indirect-object (IO) syntax was used, then the
- object is probably the correct type and there's no need to become paranoid
- about it. Perl isn't a paranoid language anyway. If people subvert the OO
- or IO syntax then they probably know what they're doing and you should let
- them do it. See rule 1.
- <P></P>
- <LI>
- Use the two-argument form of bless(). Let a subclass use your constructor.
- See <A HREF="#inheriting a constructor">INHERITING A CONSTRUCTOR</A>.
- <P></P>
- <LI>
- The subclass is allowed to know things about its immediate superclass, the
- superclass is allowed to know nothing about a subclass.
- <P></P>
- <LI>
- Don't be trigger happy with inheritance. A ``using'', ``containing'', or
- ``delegation'' relationship (some sort of aggregation, at least) is often more
- appropriate. See <A HREF="#object relationships">OBJECT RELATIONSHIPS</A>, <A HREF="#using relationship with sdbm">USING RELATIONSHIP WITH SDBM</A>,
- and <A HREF="#delegation">DELEGATION</A>.
- <P></P>
- <LI>
- The object is the namespace. Make package globals accessible via the
- object. This will remove the guess work about the symbol's home package.
- See <A HREF="#class context and the object">CLASS CONTEXT AND THE OBJECT</A>.
- <P></P>
- <LI>
- IO syntax is certainly less noisy, but it is also prone to ambiguities that
- can cause difficult-to-find bugs. Allow people to use the sure-thing OO
- syntax, even if you don't like it.
- <P></P>
- <LI>
- Do not use function-call syntax on a method. You're going to be bitten
- someday. Someone might move that method into a superclass and your code
- will be broken. On top of that you're feeding the paranoia in rule 2.
- <P></P>
- <LI>
- Don't assume you know the home package of a method. You're making it
- difficult for someone to override that method. See <A HREF="#thinking of code reuse">THINKING OF CODE REUSE</A>.
- <P></P></OL>
- <P>
- <HR>
- <H1><A NAME="instance variables">INSTANCE VARIABLES</A></H1>
- <P>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, $type;
- }</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, $type;
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = Foo->new( 'High' => 42, 'Low' => 11 );
- print "High=$a->{'High'}\n";
- print "Low=$a->{'Low'}\n";</PRE>
- <PRE>
- $b = Bar->new( 'Left' => 78, 'Right' => 40 );
- print "Left=$b->[0]\n";
- print "Right=$b->[1]\n";</PRE>
- <P>
- <HR>
- <H1><A NAME="scalar instance variables">SCALAR INSTANCE VARIABLES</A></H1>
- <P>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, $type;
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = Foo->new( 42 );
- print "a=$$a\n";</PRE>
- <P>
- <HR>
- <H1><A NAME="instance variable inheritance">INSTANCE VARIABLE INHERITANCE</A></H1>
- <P>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 $type = shift;
- my $self = {};
- $self->{'buz'} = 42;
- bless $self, $type;
- }</PRE>
- <PRE>
- package Foo;
- @ISA = qw( Bar );</PRE>
- <PRE>
- sub new {
- my $type = shift;
- my $self = Bar->new;
- $self->{'biz'} = 11;
- bless $self, $type;
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = Foo->new;
- print "buz = ", $a->{'buz'}, "\n";
- print "biz = ", $a->{'biz'}, "\n";</PRE>
- <P>
- <HR>
- <H1><A NAME="object relationships">OBJECT RELATIONSHIPS</A></H1>
- <P>The following demonstrates how one might implement ``containing'' and ``using''
- relationships between objects.</P>
- <PRE>
- package Bar;</PRE>
- <PRE>
- sub new {
- my $type = shift;
- my $self = {};
- $self->{'buz'} = 42;
- bless $self, $type;
- }</PRE>
- <PRE>
- package Foo;</PRE>
- <PRE>
- sub new {
- my $type = shift;
- my $self = {};
- $self->{'Bar'} = Bar->new;
- $self->{'biz'} = 11;
- bless $self, $type;
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = Foo->new;
- print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
- print "biz = ", $a->{'biz'}, "\n";</PRE>
- <P>
- <HR>
- <H1><A NAME="overriding superclass methods">OVERRIDING SUPERCLASS METHODS</A></H1>
- <P>The following example demonstrates how to override a superclass method and
- then call the overridden method. The <STRONG>SUPER</STRONG> pseudo-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 );</PRE>
- <PRE>
- sub new {
- my $type = shift;
- bless [], $type;
- }
- sub grr { print "grumble\n" }
- sub goo {
- my $self = shift;
- $self->SUPER::goo();
- }
- sub mumble {
- my $self = shift;
- $self->SUPER::mumble();
- }
- sub google {
- my $self = shift;
- $self->SUPER::google();
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $foo = Foo->new;
- $foo->mumble;
- $foo->grr;
- $foo->goo;
- $foo->google;</PRE>
- <P>
- <HR>
- <H1><A NAME="using relationship with sdbm">USING RELATIONSHIP WITH SDBM</A></H1>
- <P>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>
- package Mydbm;</PRE>
- <PRE>
- require SDBM_File;
- require Tie::Hash;
- @ISA = qw( Tie::Hash );</PRE>
- <PRE>
- sub TIEHASH {
- my $type = shift;
- my $ref = SDBM_File->new(@_);
- bless {'dbm' => $ref}, $type;
- }
- 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;
- use Fcntl qw( O_RDWR O_CREAT );</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>
- <P>
- <HR>
- <H1><A NAME="thinking of code reuse">THINKING OF CODE REUSE</A></H1>
- <P>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>
- <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 <CODE>BAZ()</CODE> method.</P>
- <PRE>
- package FOO;</PRE>
- <PRE>
- sub new {
- my $type = shift;
- bless {}, $type;
- }
- 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>
- <P>Now we try to override the <CODE>BAZ()</CODE> method. We would like FOO::bar() to call
- GOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
- FOO::private::BAZ().</P>
- <PRE>
- package FOO;</PRE>
- <PRE>
- sub new {
- my $type = shift;
- bless {}, $type;
- }
- 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 {
- my $type = shift;
- bless {}, $type;
- }</PRE>
- <PRE>
- sub BAZ {
- print "in GOOP::BAZ\n";
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = GOOP->new;
- $a->bar;</PRE>
- <P>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 {
- my $type = shift;
- bless {}, $type;
- }
- 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 {
- my $type = shift;
- bless {}, $type;
- }
- sub BAZ {
- print "in GOOP::BAZ\n";
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = GOOP->new;
- $a->bar;</PRE>
- <P>
- <HR>
- <H1><A NAME="class context and the object">CLASS CONTEXT AND THE OBJECT</A></H1>
- <P>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>
- <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>
- <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 just to 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 $type = shift;
- my $self = {};
- $self->{'fizzle'} = \%fizzle;
- bless $self, $type;
- }</PRE>
- <PRE>
- sub enter {
- my $self = shift;</PRE>
- <PRE>
- # 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>
- <PRE>
- print "The word is ", $fizzle->{'Password'}, "\n";
- }</PRE>
- <PRE>
- package Foo;
- @ISA = qw( Bar );</PRE>
- <PRE>
- %fizzle = ( 'Password' => 'Rumple' );</PRE>
- <PRE>
- sub new {
- my $type = shift;
- my $self = Bar->new;
- $self->{'fizzle'} = \%fizzle;
- bless $self, $type;
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = Bar->new;
- $b = Foo->new;
- $a->enter;
- $b->enter;</PRE>
- <P>
- <HR>
- <H1><A NAME="inheriting a constructor">INHERITING A CONSTRUCTOR</A></H1>
- <P>An inheritable constructor should use the second form of <A HREF="../../lib/Pod/perlfunc.html#item_bless"><CODE>bless()</CODE></A> which allows
- blessing directly into a specified class. Notice in this example that the
- object will be a BAR not a FOO, even though the constructor is in class FOO.</P>
- <PRE>
- package FOO;</PRE>
- <PRE>
- sub new {
- my $type = shift;
- my $self = {};
- bless $self, $type;
- }</PRE>
- <PRE>
- sub baz {
- print "in FOO::baz()\n";
- }</PRE>
- <PRE>
- package BAR;
- @ISA = qw(FOO);</PRE>
- <PRE>
- sub baz {
- print "in BAR::baz()\n";
- }</PRE>
- <PRE>
- package main;</PRE>
- <PRE>
- $a = BAR->new;
- $a->baz;</PRE>
- <P>
- <HR>
- <H1><A NAME="delegation">DELEGATION</A></H1>
- <P>Some classes, such as SDBM_File, cannot be effectively subclassed because
- they create foreign objects. Such a class can be extended with some sort of
- aggregation technique such as the ``using'' relationship mentioned earlier or
- by delegation.</P>
- <P>The following example demonstrates delegation using an <CODE>AUTOLOAD()</CODE> function to
- perform message-forwarding. This will allow the Mydbm object to behave
- exactly like an SDBM_File object. The Mydbm class could now extend the
- behavior by adding custom <CODE>FETCH()</CODE> and <CODE>STORE()</CODE> methods, if this is desired.</P>
- <PRE>
- package Mydbm;</PRE>
- <PRE>
- require SDBM_File;
- require Tie::Hash;
- @ISA = qw(Tie::Hash);</PRE>
- <PRE>
- sub TIEHASH {
- my $type = shift;
- my $ref = SDBM_File->new(@_);
- bless {'delegate' => $ref};
- }</PRE>
- <PRE>
- sub AUTOLOAD {
- my $self = shift;</PRE>
- <PRE>
- # The Perl interpreter places the name of the
- # message in a variable called $AUTOLOAD.</PRE>
- <PRE>
- # DESTROY messages should never be propagated.
- return if $AUTOLOAD =~ /::DESTROY$/;</PRE>
- <PRE>
- # Remove the package name.
- $AUTOLOAD =~ s/^Mydbm:://;</PRE>
- <PRE>
- # Pass the message to the delegate.
- $self->{'delegate'}->$AUTOLOAD(@_);
- }</PRE>
- <PRE>
- package main;
- use Fcntl qw( O_RDWR O_CREAT );</PRE>
- <PRE>
- tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
- $foo{'bar'} = 123;
- print "foo-bar = $foo{'bar'}\n";</PRE>
- <TABLE BORDER=0 CELLPADDING=0 CELLSPACING=0 WIDTH=100%>
- <TR><TD CLASS=block VALIGN=MIDDLE WIDTH=100% BGCOLOR="#cccccc">
- <STRONG><P CLASS=block> perlbot - Bag'o Object Tricks</P></STRONG>
- </TD></TR>
- </TABLE>
-
- </BODY>
-
- </HTML>
-