If you really want to do this, here's one way. You just need to suck out the contents of your prototype hash and use it in the newly constructed object. For example:

package Foo; sub new { my ($proto, %args) = @_; my $class = ref($proto) || $proto; $proto = ref($proto) ? $proto : {}; $proto->{Foo} = $args{Foo}; return bless {%$proto}, $class; } package Bar; sub new { my ($proto, %args) = @_; my $class = ref($proto) || $proto; $proto = ref($proto) ? $proto : {}; $proto->{Bar} = $args{Bar}; return bless {%$proto}, $class; } package FooBar; use base qw(Foo Bar); sub new { my ($proto, %args) = @_; my $self = $proto->Foo::new(%args)->Bar::new(%args); $self->{FooBar} = $args{FooBar}; return($self); }; package main; use Test::More 'no_plan'; my $fb = FooBar->new( Foo => 1, Bar => 2, FooBar => 3); isa_ok($fb, 'FooBar'); is($fb->{Foo}, 1, "Foo set"); is($fb->{Bar}, 2, "Bar set"); is($fb->{FooBar}, 3, "FooBar set");

However, I would guess what you actually need is some sort of object delegation. Is your Subclass actually "kind of" PClass1 object and a "kind of" PClass2 object? Or does it just use them?

If the latter, then you might want to take a look at Class::Delegation, Class::Facade and the section on Aggregation in perltoot.

I think it was Bertrand Meyer who said something like "You have to be careful with multiple inheritence, otherwise you get a car-owner with four wheels". :-)


In reply to Re: Multiple inheritance with multiple contructors? by adrianh
in thread Multiple inheritance with multiple contructors? by bizzach

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.