http://www.perlmonks.org?node_id=189033


in reply to Multiple inheritance with multiple contructors?

Try separating instantiation from initialization:
package foo; use strict; sub new { print "foo::new\n"; my $self = bless( {}, shift ); $self->init(@_); return $self; } sub init { print "foo::init\n"; my $self = shift; my %args = ( foo=>undef, @_ ); $args{foo} = "foo" if ! defined $args{foo}; $self->foo($args{foo}); } sub foo { my $self = shift; $self->{_foo} = shift if @_; return $self->{_foo}; } 1;

package bar; use strict; sub new { print "bar::new\n"; my $self = bless( {}, shift ); $self->init(@_); return $self; } sub init { print "bar::init\n"; my $self = shift; my %args = ( bar=>undef, @_ ); $args{bar} = "bar" if ! defined $args{bar}; $self->bar($args{bar}); } sub bar { my $self = shift; $self->{_bar} = shift if @_; return $self->{_bar}; } 1;

package foobar; use strict; use foo; use bar; use vars(qw(@ISA)); @ISA=qw(foo bar); sub new { print "foobar::new\n"; my $self = bless( {}, shift ); $self->init(@_); return $self; } sub init { print "foobar::init\n"; my $self = shift; my %args = ( @_ ); $self->foo::init(%args); $self->bar::init(%args); } sub getFooBar { my $self = shift; return $self->foo() . $self->bar(); } 1;

use strict; use foobar; main(); sub main() { my $fb = new foobar( foo=>'Lance', bar=>'Deeply' ); print $fb->getFooBar() ."\n"; }