in reply to Point of AUTOLOAD in a database environment?
The above code (which is untested, so be warned) implements read accessor type functions. Possibilities explored in the book mentioned include expanding it to get/set accessors and "caching" the function by playing symbol table games, so you only have to go once through the expensive AUTOLOAD mechanism.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}; }
CU
Robartes-
Update: To change this to a set type accessor, just change do_get_... etc. to a call to your function that sets the value in the DB (the value is passed in as an argument). You can also define accessor names like set_value and filter out the set_ with a regexp in the AUTOLOAD method.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Point of AUTOLOAD in a database environment?
by hossman (Prior) on Nov 15, 2002 at 01:28 UTC |