opensourcer has asked for the wisdom of the Perl Monks concerning the following question:

#!/usr/bin/perl package Agent; use _Initialize; @ISA = qw( _Initialize tools ); sub _init { my ($self, %args) = @_; $self->tools::_init(%args); #$self->PunMeister::_init(%args); $self->{_acolytes} = $args{followers}; $self->{_philosophy} = $args{manifesto}; } sub DESTROY { my ($self) = @_; print "pg dtor\n"; no strict "refs"; foreach my $parent ( @{ref($self)."::ISA"} ) { my $destructor = $parent."::DESTROY"; $self->$destructor(); } } 1;
package _Initialize; use strict; sub new { my ($class, %args) = @_; my $self = bless {}, ref($class)||$class; $self->_init(%args); return $self; } sub DESTROY {} 1;
#!/usr/bin/perl package tools; @ISA = qw( _Initialize ); sub _init { my ($self, %args) = @_; $self->{_name} = $args{name}; $self->{_alias} = $args{moniker}; $self->{_langs} = $args{languages}; } 1;
#!/usr/bin/perl use Data::Dumper; use Agent; my $config = Agent->new(); print Dumper($config);
while trying this i got error :
Can't locate object method "new" via package "Agent" at test.pl line 10.
and if i add use _Initailize to Agent, i get another error :
Can't locate object method "_init" via package "tools" (perhaps you forgot to lo ad "tools"?) at Agent.pm line 10. pg dtor

2006-11-11 Retitled by g0n, as per Monastery guidelines
Original title: 'Inheritance'

Replies are listed 'Best First'.
Re: Implementing inheritance in Perl
by ikegami (Patriarch) on Nov 08, 2006 at 07:06 UTC

    Can't locate object method "new" via package "Agent" at test.pl line 10.

    The code you provided does not give this error. It doesn't even have 10 lines.

    Can't locate object method "_init" via package "tools" (perhaps you forgot to lo ad "tools"?) at Agent.pm line 10

    Like the error message says, you didn't load tools with use.

    A reply falls below the community's threshold of quality. You may see it by logging in.