mnology has asked for the wisdom of the Perl Monks concerning the following question:
Reverse inheritance might not be the term I'm after, please don't let it be a factor in what I'm trying to solve. With the method outlined below(and in PBP) you're expected to create an object of a 'child' package which automagically instantiates and provides access to methods of an object of the base package. In fact I want the complete opposite. I'd like to instantiate a new object of the base package, and have access to the methods in the 'child' packages' plus the base package. Is there a way to do this using Class::Std that's flying over my head?
package Package; use Class::Std; { my %trunk_of :ATTRS( :get<trunk> init_arg => 'trunk' ); sub BUILD { my ($self, $ident, $arg_ref) = @_; return; } } package Package::Branch; use base qw(Package); use Class::Std; { my %branch_of :ATTRS( :get<branch> init_arg => 'branch' ); my %trees_of :ATTRS( :get<trees> ); sub BUILD { my ($self, $ident, $arg_ref) = @_; return; } sub START { my ($self, $ident, $arg_ref) = @_; $trees_of{$ident} = 'lots of ' . $self->get_branch . 'es and' . $self->get_trunk . 's'; } }
This way works, but is not what is wanted
use Package::Branch; my $obj = Package::Branch->new( { trunk => 'trunk', branch => 'branch' } ); print "'$obj->get_trunk'\n"; print "'$obj->get_branch'\n"; print "'$obj->get_trees'\n";
Output:
'trunk'
'branch'
'lots of branches and trees'
Where I'd rather
my $obj = Package->new( { trunk => 'trunk', branch => 'branch' } );
and use the same method as before.
I can access all the methods I want to by doing:
my $obj; $obj = Package->new( { trunk => 'trunk', branch => 'branch' } ); $obj = Package::Branch->new( { trunk => 'trunk', branch => 'branch' } );
But that just seems wrong; is it?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Class::Std and Reverse inheritance?
by chromatic (Archbishop) on Mar 15, 2006 at 21:41 UTC | |
|
Re: Class::Std and Reverse inheritance?
by diotalevi (Canon) on Mar 15, 2006 at 21:47 UTC | |
|
Re: Class::Std and Reverse inheritance?
by duckyd (Hermit) on Mar 15, 2006 at 21:44 UTC |