in reply to Re^3: a simple matter of elegance
in thread a simple matter of elegance

This is the way to go, but make sure to check that the sub exists.

...building on jeanluca's code and is equally untested:
sub AUTOLOAD { my ($self, $field ) = @_ ; (my $sub = $AUTOLOAD) =~ s/.*::// ; return (exists($self->{'data'}->{$sub}) ? $self->{'data'}->{$sub} : + undef) ; }
Of course, you could fail loudly if referencing a key that is not there. You may also define a set of "public" fields that you allow in an array, and return the value of this field only if it is in that public set ... it depends on how protected you want to make the data.
my @public_fields = qw(address phone); sub in_array { ... } sub AUTOLOAD { my ($self, $field ) = @_ ; (my $sub = $AUTOLOAD) =~ s/.*::// ; return (in_array(\@public_fields,$sub) && exists($self->{'data'}->{ +$sub}) ? $self->{'data'}->{$sub} : undef) ; }