
perlbug-followup at perl
Apr 28, 2004, 11:12 AM
Post #1 of 2
(63 views)
Permalink
|
|
[perl #29230] Class::Struct, accessor overrides not called from constructor
|
|
# New Ticket Created by Rhesa Rozendaal # Please include the string: [perl #29230] # in the subject line of all future correspondence about this issue. # <URL: http://rt.perl.org:80/rt3/Ticket/Display.html?id=29230 > This is a bug report for perl from perl [at] rhesa, generated with the help of perlbug 1.34 running under perl v5.8.2. ----------------------------------------------------------------- Class::Struct allows you to override the accessors it creates, but it doesn't call them in its constructor. In other words, $struct->field('blah'); calls my override, but $struct = structure->new('field' => 'blah'); doesn't. Class::Struct simply does $r->{'field'} = $init{'field'} but it would be more useful if it did $r->field($init{'field'}) See the sample code below for a demonstration. I have also attached a diff with fixes to the code, and the results from running Struct.t, the test script for Class::Struct as found in the perl 5.8.4 distribution. Thank you for your attention, Rhesa Rozendaal Demonstration script: #!/usr/bin/perl use strict; use warnings; use Class::Struct; use Data::Dumper; ### define struct struct memory => { swap => '$', free => '$', }; ### override accessor for free sub memory::free { my $r = shift; die 'Too many args to free' if @_ > 1; if(@_) { my $id = shift; if( $id > 100 or $id < 0 ) { warn "free `$id` should be between 0 and 100"; } else { $r->{'memory::free'} = $id; } } $r->{'memory::free'}; } ### create object and dump my $m = memory->new(free=>110, swap => 100); print Dumper($m); # free contains 110, but undef is expected $m->free(120); # warns and doesn't update, as expected print Dumper($m); $m->free(25); # sets free to 25 print Dumper($m); __END__; diff of fixed code: Compare: (<)D:\APM\Perl\lib\Class\Struct.pm (20512 bytes) with: (>)D:\APM\Perl\lib\Class\Struct_new.pm (21188 bytes) 133a133,135 > > $out .= " bless \$r, \$class;\n\n"; > 153c156 < $out .= " \$r->$elem = $init [];$cmt\n"; --- > $out .= " \$r->$name( $init [] );$cmt\n"; 159c162 < $out .= " \$r->$elem = $init {};$cmt\n"; --- > $out .= " \$r->$name( $init {} );$cmt\n"; 163c166 < $out .= " \$r->$elem = $init undef;$cmt\n"; --- > $out .= " \$r->$name( $init undef );$cmt\n"; 168c171 < $out .= " { \$r->$elem = $type->new(\%{\$init{'$name'}}) } $cmt\n"; --- > $out .= " { \$r->$name( $type->new(\%{\$init{'$name'}}) ) } $cmt\n"; 170c173 < $out .= " { \$r->$elem = \$init{'$name'} } $cmt\n"; --- > $out .= " { \$r->$name( \$init{'$name'} ) } $cmt\n"; 181c184 < $out .= " bless \$r, \$class;\n }\n"; --- > $out .= "\n \$r;\n }\n"; Test results: 1..24 ok 1 - The object isa MyObj ok 2 ok 3 - The object isa ARRAY ok 4 ok 5 ok 6 - The object isa HASH ok 7 ok 8 ok 9 ok 10 - The object isa aClass ok 11 ok 12 - The object isa MyOther ok 13 ok 14 - The object isa ARRAY ok 15 ok 16 ok 17 - The object isa HASH ok 18 ok 19 ok 20 ok 21 - The object isa aClass ok 22 ok 23 ok 24 - The object isa RecClass ----------------------------------------------------------------- --- Flags: category=library severity=low --- Site configuration information for perl v5.8.2: Configured by rhesa at Wed Dec 31 09:46:19 2003. Summary of my perl5 (revision 5 version 8 subversion 2) configuration: Platform: osname=MSWin32, osvers=4.0, archname=MSWin32-x86-multi-thread uname='' config_args='undef' hint=recommended, useposix=true, d_sigaction=undef usethreads=undef use5005threads=undef useithreads=define usemultiplicity=define useperlio=define d_sfio=undef uselargefiles=define usesocks=undef use64bitint=undef use64bitall=undef uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cl', ccflags ='-nologo -Gf -W3 -MD -DNDEBUG -O1 -DWIN32 -D_CONSOLE -DNO_STRICT -DHAVE_DES_FCRYPT -DNO_HASH_SEED -DPERL_IMPLICIT_CONTEXT -DPERL_IMPLICIT_SYS -DUSE_PERLIO -DPERL_MSVCRT_READFIX', optimize='-MD -DNDEBUG -O1', cppflags='-DWIN32' ccversion='', gccversion='', gccosandvers='' intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234 d_longlong=undef, longlongsize=8, d_longdbl=define, longdblsize=10 ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='__int64', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='link', ldflags ='-nologo -nodefaultlib -release -libpath:"d:\apm\Perl\lib\CORE" -machine:x86' libpth=\lib libs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt.lib perllibs= oldnames.lib kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib netapi32.lib uuid.lib wsock32.lib mpr.lib winmm.lib version.lib odbc32.lib odbccp32.lib msvcrt.lib libc=msvcrt.lib, so=dll, useshrplib=yes, libperl=perl58.lib gnulibc_version='undef' Dynamic Linking: dlsrc=dl_win32.xs, dlext=dll, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags='-dll -nologo -nodefaultlib -release -libpath:"d:\apm\Perl\lib\CORE" -machine:x86' Locally applied patches: ACTIVEPERL_LOCAL_PATCHES_ENTRY 21846 Configure gets d_u32align wrong 21739 [perl #24493] install.html not working 21737 Ooops. left an XXX comment in, and worse still it's a // comment 21735 utf8 keys now work for tied hashes 21734 Accessing unicode keys in tie hashes via hv_exists was broken 21733 ext/threads/t/problem.t 21732 Config::myconfig() fails under ithreads 21728 Update perlhist with 5.6.2 21723 Include 'SCCS' in the list of dir names ignored by installperl 21718 Empty subroutine as object method segfaults in 5.8.2 (sometimes) 21714 Fix bug #24380: assigning list with duplicated keys to a hash 21706 [perl #24460] [DOC PATCH] the begincheck program 21693 must copy changes from win32/makeifle.mk to wince/makefile.ce 21691 Update the list of pumpkings in perlhist.pod 21687 [PATCH 5.6.2-RC1 pod/perlhist.pod] Updated 21677 OS/2 docu 21676 Bug #24407: key for shared hash got stringified into wrong pool 21673 Be sure to use -fPIC not -fpic on Linux/SPARC 21672 extending the hash attack test 21671 Benchmark.pm cmpthese segfault 21662 'make minitest' fails for op/cproto and op/pat 21586 Comment that this 'optimisation' is actually a necessary fixup 21548 Sync with Pod::Perldoc 3.12 21540 Fix backward-compatibility issues in if.pm --- @INC for perl v5.8.2: D:/APM/Perl/lib D:/APM/Perl/site/lib . --- Environment for perl v5.8.2: HOME=c:/ LANG=NL LANGUAGE (unset) LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=c:\program files\imagemagick-5.5.7-q8;C:\Cygwin\bin;C:\PerlForDotNet\bin\;C:\Program Files\Microsoft.Net\FrameworkSDK\Bin\;C:\WINNT\Microsoft.NET\Framework\v1.0.2204\;C:\Perl\bin\;C:\WINNT\system32;C:\WINNT;C:\WINNT\System32\Wbem;C:\Program Files\Microsoft SDK for Java 4.0\Bin;C:\Program Files\InstallShield\InstallShield 5.5 Professional Edition\Program;C:\MSSQL7\BINN;C:\Program Files\cvsnt;c:\data\rrdtool\bin;c:\program files\kevterm;C:\GnuPG;C:\Program Files\ImageMagick-5.5.6-Q8;C:\Program Files\Microsoft Visual Studio\Common\Tools\WinNT;C:\Program Files\Microsoft Visual Studio\Common\MSDev98\Bin;C:\Program Files\Microsoft Visual Studio\Common\Tools;C:\Program Files\Microsoft Visual Studio\VC98\bin;C:\Program Files\deskwin\shortcuts PERLDB_OPTS=RemotePort=127.0.0.1:2000 PERL_BADLANG (unset) SHELL (unset)
|