sub AUTOLOAD { my $self=shift; my $attr=$AUTOLOAD; unless (exists $allowed_attrs{$attr}) die "Hey, that attribute does not exist!\n"; # %allowed_attrs lists the attributes you are allowed to access unless (exists $self->{$attr}) do_get_the_value_from_db(); # do_... would get the value and store it in $self->{$attr}; return $self->{$attr}; }