in reply to a simple matter of elegance

I'm not sure I understand your question correctly, but I guess you just need samething like
sub data { my ( $self, $attr ) = @_ ; return $self->{'data'}->{$attr} ; }

Cheers
LuCa

Replies are listed 'Best First'.
Re^2: a simple matter of elegance
by spiros (Beadle) on Oct 03, 2007 at 10:15 UTC
    Apologies, I should have been more clear. I want the sub names to be identical to the keys of the attributes which they fetch. For example:
    sub name would return $self->{'data'}->{'name'} sub address would return $self->{'data'}->{'address'}
    etc
      maybe perlsub can help
      sub AUTOLOAD { my ($self, $field ) = @_ ; (my $sub = $AUTOLOAD) =~ s/.*::// ; return $self->{'data'}->{$sub} ; }
      The code is not tested!

      LuCa
        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) ; }