Or you could just use 5.10.x and use Hash::Util::FieldHash::fieldhash. Then the work needed in CLONE and DESTROY will be done for you, and you don't need refaddr either. Just use the reference itself as the hashkey.
Hash::Util::FieldHash makes Inside-Out Objects much easier to work with. With it, I would write your example as:
package MyMod; use strict; use warnings; use Carp; use Hash::Util::FieldHash qw[fieldhash]; fieldhash my %attr1; fieldhash my %attr2; my %hashrefs = ( attr1 => \%attr1, attr2 => \%attr2, ); sub new { my $class = shift; my %args = @_; my $self = bless do {\my $dummy}, $class; $attr1{$self} = $args{attr1}; $attr2{$self} = $args{attr2}; $self; } sub set { my ($self, $attr, $value) = @_; if (exists $hashrefs{$attr}) { $hashrefs{$attr}{$self} = $value } else { carp "Invalid attribute name $attr"; } } sub get { my ($self, $attr) = @_; if (exists $hashrefs{$attr}) { return $hashrefs{$attr}{$self} } else { carp "Invalid attribute name $attr"; return; } } 1;
In reply to Re^2: Prevent direct acces to object's attributes
by JavaFan
in thread Prevent direct acces to object's attributes
by vitoco
For: | Use: | ||
& | & | ||
< | < | ||
> | > | ||
[ | [ | ||
] | ] |