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

[perl #21787] Problem in 5.8.0 fields/base pragmata

Thread Next
April 1, 2003 10:08
[perl #21787] Problem in 5.8.0 fields/base pragmata
Message ID:
# New Ticket Created by 
# Please include the string:  [perl #21787]
# in the subject line of all future correspondence about this issue. 
# <URL: >

This is a bug report for perl from,
generated with the help of perlbug 1.34 running under perl v5.8.0.

[Please enter your report here]

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).

Complete script demonstrating the problem:

#!/usr/bin/perl -w

    package Foo;
    use strict;
    use fields qw(_foo);
    sub new {
	my $in = shift;
	my $class = ref $in || $in;
	my Foo $self = fields::new($class);
	$self->{_foo} = "This is _foo in Foo";
	return $self;

    sub get_foo {
	my $self = shift;
	return $self->{_foo};

    package Bar;
    use strict;
    use base 'Foo';
    use fields qw(_bar _baz);
    sub new {
	my $in = shift;
	my $class = ref $in || $in;
	my Bar $self = $class->SUPER::new();
	$self->{_bar} = "This is _bar in Bar";
	$self->{_baz} = "This is _baz in Bar";
	return $self;

    sub get_bar {
	my $self = shift;
	return $self->{_bar};

    sub get_baz {
	my $self = shift;
	return $self->{_baz};

package main;
use strict;

my Foo $var = Foo->new;
print "Foo->get_foo(): " . $var->get_foo() . "\n";

my Bar $var2 = Bar->new();
print "Bar->get_foo(): " . $var2->get_foo() . "\n";
print "Bar->get_bar(): " . $var2->get_bar() . "\n";
print "Bar->get_baz(): " . $var2->get_baz() . "\n";


Produces this output:

  Foo->get_foo(): This is _foo in Foo
  Bar->get_foo(): This is _bar in Bar
  Bar->get_bar(): This is _bar in Bar
  Bar->get_baz(): This is _baz in Bar

Clearly wrong. The Foo fields are getting clobbered by the Bar fields, somehow.

[Please do not change anything below this line]
Site configuration information for perl v5.8.0:

Configured by talkasab at Sat Mar  1 12:43:48 EST 2003.

Summary of my perl5 (revision 5.0 version 8 subversion 0) configuration:
    osname=linux, osvers=2.4.19-gentoo-r10, archname=i686-linux
    uname='linux alfred 2.4.19-gentoo-r10 #1 tue dec 3 10:28:53 est 2002 i686 intel(r) pentium(r) 4 cpu 2.00ghz genuineintel gnulinux '
    config_args='-des -Darchname=i686-linux -Dcc=gcc -Dprefix=/usr -Dvendorprefix=/usr -Dsiteprefix=/usr -Dlocincpth=  -Doptimize=-march=pentium4 -O3 -pipe -fomit-frame-pointer -Duselargefiles -Dd_dosuid -Dd_semctl_semun -Dscriptdir=/usr/bin -Dman3ext=3pm -Dcf_by=Gentoo -Ud_csh -Di_gdbm -Di_db -Di_ndbm'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
    cc='gcc', ccflags ='-DPERL5 -fno-strict-aliasing -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
    optimize='-march=pentium4 -O3 -pipe -fomit-frame-pointer',
    cppflags='-DPERL5 -DPERL5 -fno-strict-aliasing'
    ccversion='', gccversion='3.2.2', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='gcc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lpthread -lnsl -lndbm -lgdbm -ldb -ldl -lm -lc -lcrypt -lutil
    perllibs=-lpthread -lnsl -ldl -lm -lc -lcrypt -lutil
    libc=/lib/, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-rdynamic'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:

@INC for perl v5.8.0:

Environment for perl v5.8.0:
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PERL_BADLANG (unset)

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