in reply to Extending objects

It's unclear to me what you really want. For now, I'll assume you want multiple inheritance. Here's how I would do it (but there are more ways to do it than there are Perl programmers)
#### Foobar.pm ##### package Foobar; use Foo; use Bar; our @ISA = qw[Foo Bar]; sub new {bless {}, shift} sub init { my $self = shift; my %args = @_; $self->Foo::init(name => $args{name}) ->Bar::init(age => $args{age}); $self; } 1; #### Foo.pm #### package Foo; sub new {bless {}, shift} sub init { my $self = shift; my %args = @_; $self->{name} = $args{name}; $self; } sub name {$_[0]{name}} 1; #### Bar.pm #### package Bar; sub new {bless {}, shift} sub init { my $self = shift; my %args = @_; $self->{age} = $args{age}; $self; } sub age {$_[0]{age}} 1; #### foobar.pl #### use Foobar; my $o = Foobar->new->init(name => 'Tom', age => 23);

Replies are listed 'Best First'.
Re^2: Extending objects
by punkish (Priest) on Jul 07, 2010 at 15:23 UTC
    Yes, multiple inheritance is the correct term, thanks. I don't know why I called id "extending."

    Question: Why are you using a separate init method outside of new? Any advantages to that?

    --

    when small people start casting long shadows, it is time to go to bed
      Question: Why are you using a separate init method outside of new? Any advantages to that?
      Because that makes multiple inheritance much easier. Assume ones Foo and Bar packages are:
      #### Foo.pm #### package Foo; sub new { my $self = bless {}, shift; my %args = @_; $self->{name} = $args{name}; $self; } sub name {$_[0]{name}} 1; #### Bar.pm #### package Bar; sub new { my $self = bless {}, shift; my %args = @_; $self->{age} = $args{age}; $self; } sub age {$_[0]{age}} 1;
      Then you don't have a method that given a reference populates it. Both new methods return their own structure. Which means the bottom class needs to create two objects (by calling new in both parent classes), and then break encapsulation by tearing the two objects apart and constructing a new one.