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". :-)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.