talkasab has asked for the wisdom of the Perl Monks concerning the following question:
I'm using the fields and base pragmata in 5.8.0 for some OO stuff I'm working on. I find the combination pretty elegant, and appreciate them very much.
However, I've encountered a small problem (that vexed me quite a bit until I figured out where the issue was). Specifically, if all of the fields in a super-class are private (i.e., their names start with underscores), then sub-classes fail to respect them when inheriting. The result is that new fields in the sub-class write themselves into the same pseudo-hash slots as the fields of the super class. If there is even one non-private field in the super-class, then the sub-class fields make room for all of the super-class fields (even the private ones).
To be more specific, if the superclass Foo starts like this:
and a subclass Bar looks like this:package Foo; use strict; use fields qw(_foo); ...
Then if you set the _bar field for a Bar instance, it clobbers whatever was in the _foo field. But if Foo was defined as this instead:package Bar; use strict; use base 'Foo'; use fields qw(_bar _baz); ...
Then Bar respects Foo's fields (even the private ones).package Foo; use strict; use fields qw(unused _foo); ...
I've put up a page with a script demonstrating the problem, and another script with the trivial fix. Output of each script is at the bottom, after __END__. You can also see the output of perl -V.
Is this a known problem? Is there a patch already available? If not, to whose attention should I bring it? It's not a big deal to work around ("unused" field, anyone?), but it seems to me that base.pm and fields.pm probably be fixed.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Bug in 5.8.0 fields/base pragmata?
by Thelonius (Priest) on Apr 01, 2003 at 15:26 UTC | |
by talkasab (Acolyte) on Apr 04, 2003 at 11:31 UTC | |
Re: Bug in 5.8.0 fields/base pragmata?
by pg (Canon) on Apr 01, 2003 at 16:00 UTC | |
by chromatic (Archbishop) on Apr 01, 2003 at 16:33 UTC | |
by pg (Canon) on Apr 02, 2003 at 03:18 UTC | |
by TimToady (Parson) on Apr 02, 2003 at 20:54 UTC | |
by Anonymous Monk on Apr 03, 2003 at 14:38 UTC | |
Re: Bug in 5.8.0 fields/base pragmata?
by schwern (Scribe) on Apr 05, 2003 at 03:22 UTC | |
by Gnezdo (Initiate) on Jul 23, 2003 at 22:36 UTC |