develooper Front page | perl.perl5.porters | Postings from March 2002 inheritance problem

Thread Next
Peter Scott
March 26, 2002 14:43
Subject: inheritance problem
Message ID:
Summary: inheriting through a module that doesn't add any fields breaks 
private fields.  I've searched diligently and can't find any mention of 
this, which surprises me; perhaps my searching skills are deficient.

Consider this program:

   package Foo;
   use fields qw(_foofield);

   sub new {
     my Foo $class = shift;
   sub fooset {
     my Foo $self = shift;
     push @{$self->{_foofield}}, @_;
   sub fooget {
     my Foo $self = shift;
   package Bar;
   use base qw(Foo);
#  use fields qw(nonsense);

   package Baz;
   use base qw(Bar);
   use fields qw(bazfield);

   sub bazmeth {
     my Baz $self = shift;
     $self->{bazfield} = shift;
my Baz $b = Baz->new;
print "Before: ", $b->fooget;
print "After: ", $b->fooget;

This (use -l) prints:

Before: foo

The private attribute is destroyed by the method call to the derived 
class.  But if the commented line is restored (or the call to bazmeth is 
removed, or the base class' field is made public), it isn't.  But you have 
to give fields a field to play with in Bar, even if you don't want any, 
because just saying "use fields" in Bar - which seems a reasonable 
requirement for field inheritance to work - isn't enough.  I tried an 
obvious change to and it didn't make any difference.

Am I doing something wrong?  If not, it seems that a documentation change 
or a code change or both are in order.

Peter Scott
Pacific Systems Design Technologies

Thread Next Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About