in reply to Re: Extending objects
in thread Extending objects

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

Replies are listed 'Best First'.
Re^3: Extending objects
by JavaFan (Canon) on Jul 07, 2010 at 15:33 UTC
    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.