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;