1N/A=head1 NAME
1N/A
1N/Aperlbot - Bag'o Object Tricks (the BOT)
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThe following collection of tricks and hints is intended to whet curious
1N/Aappetites about such things as the use of instance variables and the
1N/Amechanics of object and class relationships. The reader is encouraged to
1N/Aconsult relevant textbooks for discussion of Object Oriented definitions and
1N/Amethodology. This is not intended as a tutorial for object-oriented
1N/Aprogramming or as a comprehensive guide to Perl's object oriented features,
1N/Anor should it be construed as a style guide. If you're looking for tutorials,
1N/Abe sure to read L<perlboot>, L<perltoot>, and L<perltooc>.
1N/A
1N/AThe Perl motto still holds: There's more than one way to do it.
1N/A
1N/A=head1 OO SCALING TIPS
1N/A
1N/A=over 5
1N/A
1N/A=item 1
1N/A
1N/ADo not attempt to verify the type of $self. That'll break if the class is
1N/Ainherited, when the type of $self is valid but its package isn't what you
1N/Aexpect. See rule 5.
1N/A
1N/A=item 2
1N/A
1N/AIf an object-oriented (OO) or indirect-object (IO) syntax was used, then the
1N/Aobject is probably the correct type and there's no need to become paranoid
1N/Aabout it. Perl isn't a paranoid language anyway. If people subvert the OO
1N/Aor IO syntax then they probably know what they're doing and you should let
1N/Athem do it. See rule 1.
1N/A
1N/A=item 3
1N/A
1N/AUse the two-argument form of bless(). Let a subclass use your constructor.
1N/ASee L<INHERITING A CONSTRUCTOR>.
1N/A
1N/A=item 4
1N/A
1N/AThe subclass is allowed to know things about its immediate superclass, the
1N/Asuperclass is allowed to know nothing about a subclass.
1N/A
1N/A=item 5
1N/A
1N/ADon't be trigger happy with inheritance. A "using", "containing", or
1N/A"delegation" relationship (some sort of aggregation, at least) is often more
1N/Aappropriate. See L<OBJECT RELATIONSHIPS>, L<USING RELATIONSHIP WITH SDBM>,
1N/Aand L<"DELEGATION">.
1N/A
1N/A=item 6
1N/A
1N/AThe object is the namespace. Make package globals accessible via the
1N/Aobject. This will remove the guess work about the symbol's home package.
1N/ASee L<CLASS CONTEXT AND THE OBJECT>.
1N/A
1N/A=item 7
1N/A
1N/AIO syntax is certainly less noisy, but it is also prone to ambiguities that
1N/Acan cause difficult-to-find bugs. Allow people to use the sure-thing OO
1N/Asyntax, even if you don't like it.
1N/A
1N/A=item 8
1N/A
1N/ADo not use function-call syntax on a method. You're going to be bitten
1N/Asomeday. Someone might move that method into a superclass and your code
1N/Awill be broken. On top of that you're feeding the paranoia in rule 2.
1N/A
1N/A=item 9
1N/A
1N/ADon't assume you know the home package of a method. You're making it
1N/Adifficult for someone to override that method. See L<THINKING OF CODE REUSE>.
1N/A
1N/A=back
1N/A
1N/A=head1 INSTANCE VARIABLES
1N/A
1N/AAn anonymous array or anonymous hash can be used to hold instance
1N/Avariables. Named parameters are also demonstrated.
1N/A
1N/A package Foo;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my %params = @_;
1N/A my $self = {};
1N/A $self->{'High'} = $params{'High'};
1N/A $self->{'Low'} = $params{'Low'};
1N/A bless $self, $type;
1N/A }
1N/A
1N/A
1N/A package Bar;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my %params = @_;
1N/A my $self = [];
1N/A $self->[0] = $params{'Left'};
1N/A $self->[1] = $params{'Right'};
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = Foo->new( 'High' => 42, 'Low' => 11 );
1N/A print "High=$a->{'High'}\n";
1N/A print "Low=$a->{'Low'}\n";
1N/A
1N/A $b = Bar->new( 'Left' => 78, 'Right' => 40 );
1N/A print "Left=$b->[0]\n";
1N/A print "Right=$b->[1]\n";
1N/A
1N/A=head1 SCALAR INSTANCE VARIABLES
1N/A
1N/AAn anonymous scalar can be used when only one instance variable is needed.
1N/A
1N/A package Foo;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self;
1N/A $self = shift;
1N/A bless \$self, $type;
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = Foo->new( 42 );
1N/A print "a=$$a\n";
1N/A
1N/A
1N/A=head1 INSTANCE VARIABLE INHERITANCE
1N/A
1N/AThis example demonstrates how one might inherit instance variables from a
1N/Asuperclass for inclusion in the new class. This requires calling the
1N/Asuperclass's constructor and adding one's own instance variables to the new
1N/Aobject.
1N/A
1N/A package Bar;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = {};
1N/A $self->{'buz'} = 42;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package Foo;
1N/A @ISA = qw( Bar );
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = Bar->new;
1N/A $self->{'biz'} = 11;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = Foo->new;
1N/A print "buz = ", $a->{'buz'}, "\n";
1N/A print "biz = ", $a->{'biz'}, "\n";
1N/A
1N/A
1N/A
1N/A=head1 OBJECT RELATIONSHIPS
1N/A
1N/AThe following demonstrates how one might implement "containing" and "using"
1N/Arelationships between objects.
1N/A
1N/A package Bar;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = {};
1N/A $self->{'buz'} = 42;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package Foo;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = {};
1N/A $self->{'Bar'} = Bar->new;
1N/A $self->{'biz'} = 11;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = Foo->new;
1N/A print "buz = ", $a->{'Bar'}->{'buz'}, "\n";
1N/A print "biz = ", $a->{'biz'}, "\n";
1N/A
1N/A
1N/A
1N/A=head1 OVERRIDING SUPERCLASS METHODS
1N/A
1N/AThe following example demonstrates how to override a superclass method and
1N/Athen call the overridden method. The B<SUPER> pseudo-class allows the
1N/Aprogrammer to call an overridden superclass method without actually knowing
1N/Awhere that method is defined.
1N/A
1N/A package Buz;
1N/A sub goo { print "here's the goo\n" }
1N/A
1N/A package Bar; @ISA = qw( Buz );
1N/A sub google { print "google here\n" }
1N/A
1N/A package Baz;
1N/A sub mumble { print "mumbling\n" }
1N/A
1N/A package Foo;
1N/A @ISA = qw( Bar Baz );
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A bless [], $type;
1N/A }
1N/A sub grr { print "grumble\n" }
1N/A sub goo {
1N/A my $self = shift;
1N/A $self->SUPER::goo();
1N/A }
1N/A sub mumble {
1N/A my $self = shift;
1N/A $self->SUPER::mumble();
1N/A }
1N/A sub google {
1N/A my $self = shift;
1N/A $self->SUPER::google();
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $foo = Foo->new;
1N/A $foo->mumble;
1N/A $foo->grr;
1N/A $foo->goo;
1N/A $foo->google;
1N/A
1N/ANote that C<SUPER> refers to the superclasses of the current package
1N/A(C<Foo>), not to the superclasses of C<$self>.
1N/A
1N/A
1N/A=head1 USING RELATIONSHIP WITH SDBM
1N/A
1N/AThis example demonstrates an interface for the SDBM class. This creates a
1N/A"using" relationship between the SDBM class and the new class Mydbm.
1N/A
1N/A package Mydbm;
1N/A
1N/A require SDBM_File;
1N/A require Tie::Hash;
1N/A @ISA = qw( Tie::Hash );
1N/A
1N/A sub TIEHASH {
1N/A my $type = shift;
1N/A my $ref = SDBM_File->new(@_);
1N/A bless {'dbm' => $ref}, $type;
1N/A }
1N/A sub FETCH {
1N/A my $self = shift;
1N/A my $ref = $self->{'dbm'};
1N/A $ref->FETCH(@_);
1N/A }
1N/A sub STORE {
1N/A my $self = shift;
1N/A if (defined $_[0]){
1N/A my $ref = $self->{'dbm'};
1N/A $ref->STORE(@_);
1N/A } else {
1N/A die "Cannot STORE an undefined key in Mydbm\n";
1N/A }
1N/A }
1N/A
1N/A package main;
1N/A use Fcntl qw( O_RDWR O_CREAT );
1N/A
1N/A tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640;
1N/A $foo{'bar'} = 123;
1N/A print "foo-bar = $foo{'bar'}\n";
1N/A
1N/A tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640;
1N/A $bar{'Cathy'} = 456;
1N/A print "bar-Cathy = $bar{'Cathy'}\n";
1N/A
1N/A=head1 THINKING OF CODE REUSE
1N/A
1N/AOne strength of Object-Oriented languages is the ease with which old code
1N/Acan use new code. The following examples will demonstrate first how one can
1N/Ahinder code reuse and then how one can promote code reuse.
1N/A
1N/AThis first example illustrates a class which uses a fully-qualified method
1N/Acall to access the "private" method BAZ(). The second example will show
1N/Athat it is impossible to override the BAZ() method.
1N/A
1N/A package FOO;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A bless {}, $type;
1N/A }
1N/A sub bar {
1N/A my $self = shift;
1N/A $self->FOO::private::BAZ;
1N/A }
1N/A
1N/A package FOO::private;
1N/A
1N/A sub BAZ {
1N/A print "in BAZ\n";
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = FOO->new;
1N/A $a->bar;
1N/A
1N/ANow we try to override the BAZ() method. We would like FOO::bar() to call
1N/AGOOP::BAZ(), but this cannot happen because FOO::bar() explicitly calls
1N/AFOO::private::BAZ().
1N/A
1N/A package FOO;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A bless {}, $type;
1N/A }
1N/A sub bar {
1N/A my $self = shift;
1N/A $self->FOO::private::BAZ;
1N/A }
1N/A
1N/A package FOO::private;
1N/A
1N/A sub BAZ {
1N/A print "in BAZ\n";
1N/A }
1N/A
1N/A package GOOP;
1N/A @ISA = qw( FOO );
1N/A sub new {
1N/A my $type = shift;
1N/A bless {}, $type;
1N/A }
1N/A
1N/A sub BAZ {
1N/A print "in GOOP::BAZ\n";
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = GOOP->new;
1N/A $a->bar;
1N/A
1N/ATo create reusable code we must modify class FOO, flattening class
1N/AFOO::private. The next example shows a reusable class FOO which allows the
1N/Amethod GOOP::BAZ() to be used in place of FOO::BAZ().
1N/A
1N/A package FOO;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A bless {}, $type;
1N/A }
1N/A sub bar {
1N/A my $self = shift;
1N/A $self->BAZ;
1N/A }
1N/A
1N/A sub BAZ {
1N/A print "in BAZ\n";
1N/A }
1N/A
1N/A package GOOP;
1N/A @ISA = qw( FOO );
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A bless {}, $type;
1N/A }
1N/A sub BAZ {
1N/A print "in GOOP::BAZ\n";
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = GOOP->new;
1N/A $a->bar;
1N/A
1N/A=head1 CLASS CONTEXT AND THE OBJECT
1N/A
1N/AUse the object to solve package and class context problems. Everything a
1N/Amethod needs should be available via the object or should be passed as a
1N/Aparameter to the method.
1N/A
1N/AA class will sometimes have static or global data to be used by the
1N/Amethods. A subclass may want to override that data and replace it with new
1N/Adata. When this happens the superclass may not know how to find the new
1N/Acopy of the data.
1N/A
1N/AThis problem can be solved by using the object to define the context of the
1N/Amethod. Let the method look in the object for a reference to the data. The
1N/Aalternative is to force the method to go hunting for the data ("Is it in my
1N/Aclass, or in a subclass? Which subclass?"), and this can be inconvenient
1N/Aand will lead to hackery. It is better just to let the object tell the
1N/Amethod where that data is located.
1N/A
1N/A package Bar;
1N/A
1N/A %fizzle = ( 'Password' => 'XYZZY' );
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = {};
1N/A $self->{'fizzle'} = \%fizzle;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A sub enter {
1N/A my $self = shift;
1N/A
1N/A # Don't try to guess if we should use %Bar::fizzle
1N/A # or %Foo::fizzle. The object already knows which
1N/A # we should use, so just ask it.
1N/A #
1N/A my $fizzle = $self->{'fizzle'};
1N/A
1N/A print "The word is ", $fizzle->{'Password'}, "\n";
1N/A }
1N/A
1N/A package Foo;
1N/A @ISA = qw( Bar );
1N/A
1N/A %fizzle = ( 'Password' => 'Rumple' );
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = Bar->new;
1N/A $self->{'fizzle'} = \%fizzle;
1N/A bless $self, $type;
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = Bar->new;
1N/A $b = Foo->new;
1N/A $a->enter;
1N/A $b->enter;
1N/A
1N/A=head1 INHERITING A CONSTRUCTOR
1N/A
1N/AAn inheritable constructor should use the second form of bless() which allows
1N/Ablessing directly into a specified class. Notice in this example that the
1N/Aobject will be a BAR not a FOO, even though the constructor is in class FOO.
1N/A
1N/A package FOO;
1N/A
1N/A sub new {
1N/A my $type = shift;
1N/A my $self = {};
1N/A bless $self, $type;
1N/A }
1N/A
1N/A sub baz {
1N/A print "in FOO::baz()\n";
1N/A }
1N/A
1N/A package BAR;
1N/A @ISA = qw(FOO);
1N/A
1N/A sub baz {
1N/A print "in BAR::baz()\n";
1N/A }
1N/A
1N/A package main;
1N/A
1N/A $a = BAR->new;
1N/A $a->baz;
1N/A
1N/A=head1 DELEGATION
1N/A
1N/ASome classes, such as SDBM_File, cannot be effectively subclassed because
1N/Athey create foreign objects. Such a class can be extended with some sort of
1N/Aaggregation technique such as the "using" relationship mentioned earlier or
1N/Aby delegation.
1N/A
1N/AThe following example demonstrates delegation using an AUTOLOAD() function to
1N/Aperform message-forwarding. This will allow the Mydbm object to behave
1N/Aexactly like an SDBM_File object. The Mydbm class could now extend the
1N/Abehavior by adding custom FETCH() and STORE() methods, if this is desired.
1N/A
1N/A package Mydbm;
1N/A
1N/A require SDBM_File;
1N/A require Tie::Hash;
1N/A @ISA = qw(Tie::Hash);
1N/A
1N/A sub TIEHASH {
1N/A my $type = shift;
1N/A my $ref = SDBM_File->new(@_);
1N/A bless {'delegate' => $ref};
1N/A }
1N/A
1N/A sub AUTOLOAD {
1N/A my $self = shift;
1N/A
1N/A # The Perl interpreter places the name of the
1N/A # message in a variable called $AUTOLOAD.
1N/A
1N/A # DESTROY messages should never be propagated.
1N/A return if $AUTOLOAD =~ /::DESTROY$/;
1N/A
1N/A # Remove the package name.
1N/A $AUTOLOAD =~ s/^Mydbm:://;
1N/A
1N/A # Pass the message to the delegate.
1N/A $self->{'delegate'}->$AUTOLOAD(@_);
1N/A }
1N/A
1N/A package main;
1N/A use Fcntl qw( O_RDWR O_CREAT );
1N/A
1N/A tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
1N/A $foo{'bar'} = 123;
1N/A print "foo-bar = $foo{'bar'}\n";
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<perlboot>, L<perltoot>, L<perltooc>.