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:

package Foo; use strict; use fields qw(_foo); ...
and a subclass Bar looks like this:
package Bar; use strict; use base 'Foo'; use fields qw(_bar _baz); ...
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 Foo; use strict; use fields qw(unused _foo); ...
Then Bar respects Foo's fields (even the private ones).

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.


In reply to Bug in 5.8.0 fields/base pragmata? by talkasab

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.