diff options
author | Rhesa Rozendaal <perl@rhesa.com> | 2012-06-15 09:39:13 -0700 |
---|---|---|
committer | Father Chrysostomos <sprout@cpan.org> | 2012-06-15 12:28:18 -0700 |
commit | a79a48c06297674602f44ec367c8b2768b1c997a (patch) | |
tree | 45f84905c39420ab2e944139b32ef3aab412fd90 /lib/Class | |
parent | 84fde9b42e7c19995ec85ea8f148b482068f4228 (diff) | |
download | perl-a79a48c06297674602f44ec367c8b2768b1c997a.tar.gz |
[perl #29230] Class::Struct, accessor overrides not called from constructor
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'})
Diffstat (limited to 'lib/Class')
-rw-r--r-- | lib/Class/Struct.pm | 16 |
1 files changed, 10 insertions, 6 deletions
diff --git a/lib/Class/Struct.pm b/lib/Class/Struct.pm index 7a9af54faf..a21812c2ec 100644 --- a/lib/Class/Struct.pm +++ b/lib/Class/Struct.pm @@ -130,6 +130,9 @@ sub struct { 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 @@ sub struct { 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 @@ sub struct { } $idx += 2; } - $out .= " bless \$r, \$class;\n }\n"; + + $out .= "\n \$r;\n}\n"; # Create accessor methods. |