On Jan 5, 2004, at 2:44 PM, Shawn McKinley wrote:

Hello all,
  I am wondering if you can have object inherited between
packages when the child packages have their own object
creation without explicitly setting the parent object in
the child?  Is there a way to inherit the parent object?
Example below (sorry for the length).

I'm wondering if you might be talking about constructor inheritance here. You can call the inherited constructor and then do your own class specific setup additions to the returned object. I'll inline an example below.


If this isn't what you're looking for though, you might try explaining the problem to us again, because frankly I didn't understand what you were asking.

TIA,
Shawn

A.pm
  package A;
  use strict;
  sub new {
    my($class)=shift;
    my $self={};
    bless $self,ref $class || $class;
    return $self;
  }

  sub func {
    my $self=shift;
    $self->{A_FUNC}=shift;
  }

  sub func2 {
    my $self=shift;
    require A::B;
    my $self->{A__B_OBJECT}=A::B->new();
    return $self->{A__B_OBJECT}->func();
  }

B.pm
  package A::B;
  A::B::ISA=qw(A);
  use strict;
  sub new {
    my($class)=shift;
    my $self={};
    bless $self,ref $class || $class;
    return $self;
  }

Replace the above with something like:


sub new {
        my $class = shift;
        my $self = $class->SUPER::new( @_ }; # call class A constructor

# class B setup goes here...

        return $self;
}

Hope that helps.

James


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to