SUPER::AUTOLOAD(@_) -- will this work? With $AUTOLOAD?
am 20.12.2007 01:01:05 von el.dodgeroHim guys,
In a nutshell, I have built a module that does some database
abstraction, called SQLUtils. This object does all the database
handling and allows me to define and store named queries.
Next, I have a sort of generic Util module that uses SQLUtils and also
defines an AUTOLOAD method that lets these named queries be called as
if they were methods. Basically, you just set up your queries in a
hash, call the addQueries method which adds them all into the SQLUtils
object inside it, and if you call an undefined method it looks for the
query and, if it finds it, calls the query and fetches the result (I'm
going to add caching of results later).
Well, now I'm writing a Member module (for registered members on a
website). I want to be able to define an AUTOLOAD for that module that
will handle returning and updating member data for any member keys
that exist, calling those like methods. However, if the key does NOT
exist in the member data, I want the *other* AUTOLOAD to run. Like so:
sub AUTOLOAD {
my $obj = shift;
my $method = $AUTOLOAD;
$method =$ s/.*://;
if (exists $obj->{data}->{$method}) {
if (@_) {
my $newval = shift;
$obj->{data}->{$method} = $newval;
$obj->sql->update($newval, $obj->{data}->{id});
return $obj->{data}->{$method};
}
}
else {
$obj->SUPER::AUTOLOAD(@_);
}
}
What I'm concerned about is this: is $AUTOLOAD going to remain? Will
it be wiped to undef if I call AUTOLOAD directly?
Or am I totally barking up the wrong tree here? I've never tried this
trick before... always just used one AUTOLOAD, either the parent's or
the class's own. Never had a need to use this class's AUTOLOAD *or*
the parent class's AUTOLOAD under certain circumstances.
I mean, yeah I could just redefine all the same stuff in the parent's
AUTOLOAD inside this AUTOLOAD's else block, but that seems inelegant.
--
Dodger