New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Class::Struct, accessor overrides not called from constructor #7272
Comments
From perl@rhesa.comThis is a bug report for perl from perl@rhesa.com, Class::Struct allows you to override the accessors it creates, but it $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. Thank you for your attention, Demonstration script: #!/usr/bin/perl ### define struct ### override accessor for free $r->{'memory::free'}; ### create object and dump $m->free(120); # warns and doesn't update, as expected $m->free(25); # sets free to 25 __END__; diff of fixed code: Compare: (<)D:\APM\Perl\lib\Class\Struct.pm (20512 bytes) 133a133,135
153c156
159c162
163c166
168c171
$type->new(\%{\$init{'$name'}}) ) } $cmt\n";
$cmt\n";
Test results: 1..24 Flags: 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: Locally applied patches: @INC for perl v5.8.2: Environment for perl v5.8.2: |
From @schwern
Sounds like a valid complaint to me. Example 2 in the docs demonstrates Pumpkings, there's a patch with this ticket to fix this. |
The RT System itself - Status changed from 'new' to 'open' |
From renee.baecker@smart-websolutions.deHi, I've tested the patch by Rhesa Rozendaal and made a unified diff (that Cheers, |
From renee.baecker@smart-websolutions.deStruct.patch--- lib/Class/Struct.pm.orig 2008-08-31 11:03:38.000000000 +0200
+++ lib/Class/Struct.pm 2008-08-31 11:11:36.000000000 +0200
@@ -130,6 +130,9 @@
elsif( $base_type eq 'ARRAY' ){
$out .= " my(\$r) = [];\n";
}
+
+ $out .= " bless \$r, \$class;\n\n";
+
while( $idx < @decls ){
$name = $decls[$idx];
$type = $decls[$idx+1];
@@ -150,24 +153,24 @@
if( $type eq '@' ){
$out .= " croak 'Initializer for $name must be array reference'\n";
$out .= " if defined(\$init{'$name'}) && ref(\$init{'$name'}) ne 'ARRAY';\n";
- $out .= " \$r->$elem = $init [];$cmt\n";
+ $out .= " \$r->$name( $init [] );$cmt\n";
$arrays{$name}++;
}
elsif( $type eq '%' ){
$out .= " croak 'Initializer for $name must be hash reference'\n";
$out .= " if defined(\$init{'$name'}) && ref(\$init{'$name'}) ne 'HASH';\n";
- $out .= " \$r->$elem = $init {};$cmt\n";
+ $out .= " \$r->$name( $init {} );$cmt\n";
$hashes{$name}++;
}
elsif ( $type eq '$') {
- $out .= " \$r->$elem = $init undef;$cmt\n";
+ $out .= " \$r->$name( $init undef );$cmt\n";
}
elsif( $type =~ /^\w+(?:::\w+)*$/ ){
$out .= " if (defined(\$init{'$name'})) {\n";
$out .= " if (ref \$init{'$name'} eq 'HASH')\n";
- $out .= " { \$r->$elem = $type->new(\%{\$init{'$name'}}) } $cmt\n";
+ $out .= " { \$r->$name( $type->new(\%{\$init{'$name'}}) ) } $cmt\n";
$out .= " elsif (UNIVERSAL::isa(\$init{'$name'}, '$type'))\n";
- $out .= " { \$r->$elem = \$init{'$name'} } $cmt\n";
+ $out .= " { \$r->$name( \$init{'$name'} ) } $cmt\n";
$out .= " else { croak 'Initializer for $name must be hash or $type reference' }\n";
$out .= " }\n";
$classes{$name} = $type;
@@ -178,7 +181,8 @@
}
$idx += 2;
}
- $out .= " bless \$r, \$class;\n }\n";
+
+ $out .= "\n \$r;\n}\n";
# Create accessor methods.
|
From @HugmeirOn Sun Aug 31 02:16:51 2008, renee.baecker@smart-websolutions.de wrote:
Could someone review/apply the patch in this ticket? --hugmeir |
From @cpansproutOn Sun May 27 04:21:46 2012, Hugmeir wrote:
It’s not only committers who can review patches. :-) I’ve never used Class::Struct before. -- Father Chrysostomos |
From @cpansproutOn Fri Jun 08 20:34:30 2012, sprout wrote:
But I’ve just had a look at the patch anyway, and it looks good, except -- Father Chrysostomos |
From mailinglisten@renee-baecker.deOn 09.06.2012 05:41, Father Chrysostomos via RT wrote:
Attached is a patch that add two tests... - Renee |
From mailinglisten@renee-baecker.de0001-add-tests-for-overridden-Class-Struct-accessors.patchFrom a06895e8db0e5e1a0de2ee2715e5fc1fd5d56de1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Renee=20B=C3=A4cker?= <perl@renee-baecker.de>
Date: Sat, 9 Jun 2012 04:17:18 -0700
Subject: [PATCH] add tests for overridden Class::Struct accessors
---
lib/Class/Struct.t | 26 +++++++++++++++++++++++++-
1 files changed, 25 insertions(+), 1 deletions(-)
diff --git a/lib/Class/Struct.t b/lib/Class/Struct.t
index 694d622..71cdaa3 100644
--- a/lib/Class/Struct.t
+++ b/lib/Class/Struct.t
@@ -33,11 +33,29 @@ package MyOther;
use Class::Struct s => '$', a => '@', h => '%', c => 'aClass';
#
+# test overriden accessors
+#
+package OverrideAccessor;
+use Class::Struct;
+
+struct( 'OverrideAccessor', { count => '$' } );
+
+sub count {
+ my ($self,$count) = @_;
+
+ if ( @_ >= 2 ) {
+ $self->{'OverrideAccessor::count'} = $count + 9;
+ }
+
+ return $self->{'OverrideAccessor::count'};
+}
+
+#
# back to main...
#
package main;
-use Test::More tests => 24;
+use Test::More tests => 26;
my $obj = MyObj->new;
isa_ok $obj, 'MyObj';
@@ -101,3 +119,9 @@ is $obk->SomeElem(), 123;
my $recobj = RecClass->new();
isa_ok $recobj, 'RecClass';
+my $override_obj = OverrideAccessor->new( count => 3 );
+is $override_obj->count, 12;
+
+$override_obj->count( 1 );
+is $override_obj->count, 10;
+
--
1.7.5.4
|
From @cpansproutOn Sat Jun 09 04:22:47 2012, mailinglisten@renee-baecker.de wrote:
Thank you. I have applied the original patch as a79a48c and the tests -- Father Chrysostomos |
@cpansprout - Status changed from 'open' to 'resolved' |
I'm using ActiveState Perl 5.20 built 20 July 2015. It has Class::Struct 0.65 and it doesn't seem to have this fix. What version of Class::Struct has this fix, and where can I get it? CPAN shows version 0.66 with no changes since 2001. Thanks. |
On Tue, May 09, 2023 at 08:44:29AM +0000, freonpsandoz wrote:
I'm using ActiveState Perl 5.20 built 20 July 2015. It has Class::Struct
0.65 and it doesn't seem to have this fix. What version of Class::Struct
has this fix, and where can I get it? CPAN shows version 0.66 with no
changes since 2001. Thanks.
The fix mentioned in this ticket was applied to the version of
Class::Struct bundled with perl - it appeared first in Class::Struct
version 0.64, which was bundled with perl 5.18.0.
…--
In my day, we used to edit the inodes by hand. With magnets.
|
Migrated from rt.perl.org#29230 (status was 'resolved')
Searchable as RT29230$
The text was updated successfully, but these errors were encountered: