Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-20 01:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found