perlbot - Bag'o Object Tricks (the BOT)
-=head1 INTRODUCTION
+=head1 DESCRIPTION
The following collection of tricks and hints is intended to whet curious
appetites about such things as the use of instance variables and the
=item 7
-IO syntax is certainly less noisy, but it is also prone to ambiguities which
+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.
=head1 OVERRIDING SUPERCLASS METHODS
-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.
-
+The following example demonstrates how to override a superclass method and
+then call the overridden method. The B<SUPER> pseudo-class allows the
+programmer to call an overridden superclass method without actually knowing
+where that method is defined.
package Buz;
sub goo { print "here's the goo\n" }
package Foo;
@ISA = qw( Bar Baz );
- @Foo::Inherit::ISA = @ISA; # Access to overridden methods.
sub new {
my $type = shift;
sub grr { print "grumble\n" }
sub goo {
my $self = shift;
- $self->Foo::Inherit::goo();
+ $self->SUPER::goo();
}
sub mumble {
my $self = shift;
- $self->Foo::Inherit::mumble();
+ $self->SUPER::mumble();
}
sub google {
my $self = shift;
- $self->Foo::Inherit::google();
+ $self->SUPER::google();
}
package main;
package Mydbm;
require SDBM_File;
- require TieHash;
- @ISA = qw( TieHash );
+ require Tie::Hash;
+ @ISA = qw( Tie::Hash );
sub TIEHASH {
my $type = shift;
$ref->FETCH(@_);
}
sub STORE {
- my $self = shift;
+ my $self = shift;
if (defined $_[0]){
my $ref = $self->{'dbm'};
$ref->STORE(@_);
package main;
use Fcntl qw( O_RDWR O_CREAT );
- tie %foo, Mydbm, "Sdbm", O_RDWR|O_CREAT, 0640;
+ tie %foo, "Mydbm", "Sdbm", O_RDWR|O_CREAT, 0640;
$foo{'bar'} = 123;
print "foo-bar = $foo{'bar'}\n";
- tie %bar, Mydbm, "Sdbm2", O_RDWR|O_CREAT, 0640;
+ tie %bar, "Mydbm", "Sdbm2", O_RDWR|O_CREAT, 0640;
$bar{'Cathy'} = 456;
print "bar-Cathy = $bar{'Cathy'}\n";
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
+and will lead to hackery. It is better just to let the object tell the
method where that data is located.
package Bar;
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.
package Mydbm;
require SDBM_File;
- require TieHash;
- @ISA = qw(TieHash);
+ require Tie::Hash;
+ @ISA = qw(Tie::Hash);
sub TIEHASH {
my $type = shift;
package main;
use Fcntl qw( O_RDWR O_CREAT );
- tie %foo, Mydbm, "adbm", O_RDWR|O_CREAT, 0640;
+ tie %foo, "Mydbm", "adbm", O_RDWR|O_CREAT, 0640;
$foo{'bar'} = 123;
print "foo-bar = $foo{'bar'}\n";