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?


In reply to Class::Std and Reverse inheritance? by mnology

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.