inheritance
Peter Scott
Peter at PSDT.com
Wed Feb 19 18:03:11 CST 2003
At 03:51 PM 2/19/03 -0800, nkuipers wrote:
>Hello,
>
>I've been playing around with inheritance and I've got a question. Let's say
>the parent class is called BIO::Basic and the child class is called
>BIO::Annotate, and each class is in a separate file.
>
>Parent constructor is as follows:
>
>sub new {
> my $caller = shift;
> my $type = ref($caller) || $caller;
> my $self = { id => 'no value',
> desc => 'no value',
> seq => 'no value',
> alphabet => 'no value',
> profile => {},
> @_ };
>.
>.
>.
>bless $self, $type;
>}
>
>Child constructor is as follows:
>
>sub new {
> my $type = shift;
> my $self = {};
> $self->{bogus_key} = BIO::Basic->new(@_);
> bless $self, $type;
>}
>
>If I just call $self = BIO::Basic->new(@_), then Annotate objects cannot use
>Basic methods. Addition of the {super} key in Annotate's self allows access
>to Basic methods in the driver script, as in:
>
>$annotate_object->{bogus_key}->parent_method();
>
>Am I missing something here? Isn't there a simpler way? I guess I'm used to
>the inheritance of java. I got the above approach from
>
>http://www.perldoc.com/perl5.6/pod/perlbot.html
The problem with learning from a Baog O' Tricks page is that some
fundamentals got glossed over. (Get Conway's book.) In this case
you've missed the way that inheritance is done in Perl using @ISA. (In
fact, perlbot ought to be updated to use "use base", which is better.)
Inheritance is easier, much easier. Here's the child:
package BIO::Annotate;
use strict;
use warnings;
use base qw(BIO::Basic);
sub new {
my $caller = shift;
my type = ref($caller) || $caller;
my $self = $caller->SUPER::new(@_);
# anything else you want to do with $self
return $self;
}
SUPER is documented in perlobj.
What you did is object "delegation". There is a raging controversy
among O-O purists about when the best time to use this is. But it's
not what you were looking for.
--
Peter Scott
Pacific Systems Design Technologies
http://www.perldebugged.com/
More information about the Victoria-pm
mailing list