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

    I don't understand why you want to do this at all. What problem are you trying to solve? What's wrong with the code you posted as not doing what you want? Are you looking for some sort of factory class or role composition with Class::Trait?

Re: Class::Std and Reverse inheritance?
by diotalevi (Canon) on Mar 15, 2006 at 21:47 UTC

    Here's some terminology for you. You could look at making Package be a factory. You might also look at removing ISA relationships and using mixins instead. I'd go with the latter given what you just described.

    package Package; use Package::Branch; ... package Package::Branch; use Exporter; *importer = \ &Exporter::importer; our @EXPORT = qw( get_branch get_trees ); ...

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Class::Std and Reverse inheritance?
by duckyd (Hermit) on Mar 15, 2006 at 21:44 UTC
    It don't think inside out objects v. traditional objects even matters. You're asking to do something that's not possible with standard OOP practices. How can a parent class know what child classes exist? If you have a solution, it will likely be applicable to inside out or traditional objects.