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

Re: Multiple inheritance with multiple contructors?

by LanceDeeply (Chaplain)
on Aug 09, 2002 at 20:36 UTC ( [id://189033]=note: print w/replies, xml ) Need Help??


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"; }

Replies are listed 'Best First'.
Re: Re: Multiple inheritance with multiple contructors?
by bizzach (Beadle) on Aug 12, 2002 at 18:25 UTC

    This works wonderfully and solves all my problems. I trully appreciate your help.
    Thanks,
    bizzach

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://189033]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (2)
As of 2024-04-24 14:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found