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).

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

  sub func {
    my $self=shift;
    $self->{A__B_FUNC}=$self->{A_FUNC}/2;
    # I know this does not work, but that
    # is why I am asking the question
  }

Versus passing the A object explicitly:

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();
    $self->{A__B_OBJECT}->set_A($self);
    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;
  }

  sub set_A {
    my $self=shift;
    $self->{A_OBJECT}=shift;
  }

  sub func {
    my $self=shift;
    $self->{A__B_FUNC}=$self->{A_OBJECT}->{A_FUNC}/2;
  }



-- 
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