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

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

Replies are listed 'Best First'.
Re^3: a simple matter of elegance
by jeanluca (Deacon) on Oct 03, 2007 at 10:33 UTC
    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) ; }