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

fields.pm inheritance problem

Thread Next
From:
Peter Scott
Date:
March 26, 2002 14:43
Subject:
fields.pm inheritance problem
Message ID:
4.3.2.7.2.20020326141406.00b52660@shell2.webquarry.com
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;
     fields::new($class);
   }
   sub fooset {
     my Foo $self = shift;
     push @{$self->{_foofield}}, @_;
   }
   sub fooget {
     my Foo $self = shift;
     @{$self->{_foofield}};
   }
   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;
$b->fooset('foo');
print "Before: ", $b->fooget;
$b->bazmeth('baz');
print "After: ", $b->fooget;

This (use -l) prints:

Before: foo
After:

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 fields.pm 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


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About