develooper Front page | perl.perl5.porters | Postings from September 2003

Re: fields.pm inheritance problem

Thread Previous | Thread Next
From:
Michael G Schwern
Date:
September 15, 2003 15:09
Subject:
Re: fields.pm inheritance problem
Message ID:
20030915110830.GA8022@windhund.schwern.org
Better late than never.  base 2.03 fixes this.  Problem was field
attribute inheritence only really happened when the base class had some
fields.  Since the middle class has no fields, information was forgotten.
I put in a special case for when there's no fields to inherit to make sure
the inherited attributes get passed along.


On Tue, Mar 26, 2002 at 02:41:24PM -0800, Peter Scott wrote:
> 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.

-- 
Michael G Schwern        schwern@pobox.com  http://www.pobox.com/~schwern/
How can you get very far,
If you don't know Who You Are?
How can you do what you ought,
If you don't know What You've Got?

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