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

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

Thread Next
From:
perlbug-followup
Date:
April 1, 2003 10:08
Subject:
[perl #21787] Problem in 5.8.0 fields/base pragmata
Message ID:
rt-21787-54444.9.74557265620163@bugs6.perl.org
# New Ticket Created by  talkasab@alfred.hsdb.tufts.edu 
# Please include the string:  [perl #21787]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt2/Ticket/Display.html?id=21787 >



This is a bug report for perl from talkasab@alfred.hsdb.tufts.edu,
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";

1;
__END__

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]
-----------------------------------------------------------------
---
Flags:
    category=library
    severity=medium
---
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:
  Platform:
    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
  Compiler:
    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/libc-2.3.1.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.3.1'
  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:
    /home/talkasab/HSDB/apache/lib/perl
    /home/talkasab/hsdb_tool/lib
    /home/talkasab/lib/perl
    /usr/lib/perl5/5.8.0/i686-linux
    /usr/lib/perl5/5.8.0
    /usr/lib/perl5/site_perl/5.8.0/i686-linux
    /usr/lib/perl5/site_perl/5.8.0
    /usr/lib/perl5/site_perl/5.6.1
    /usr/lib/perl5/site_perl
    /usr/lib/perl5/vendor_perl/5.8.0/i686-linux
    /usr/lib/perl5/vendor_perl/5.8.0
    /usr/lib/perl5/vendor_perl
    .

---
Environment for perl v5.8.0:
    HOME=/home/talkasab
    LANG (unset)
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/home/talkasab/bin:/home/talkasab/hsdb_tool/bin:/bin:/usr/bin:/usr/local/bin:/opt/bin:/usr/i686-pc-linux-gnu/gcc-bin/3.2:/usr/X11R6/bin:/opt/sun-j2sdk-1.4.1/bin:/opt/sun-j2sdk-1.4.1/jre/bin:/usr/qt/3/bin:/usr/kde/3.1/bin:/usr/qt/2/bin:/opt/vmware/bin
    PERL5LIB=/home/talkasab/HSDB/apache/lib/perl:/home/talkasab/hsdb_tool/lib:/home/talkasab/lib/perl
    PERL_BADLANG (unset)
    SHELL=/bin/zsh


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