|Perl: the Markov chain saw|
AUTOLOAD for variables?by dpuu (Chaplain)
|on Jul 07, 2004 at 23:55 UTC||Need Help??|
dpuu has asked for the wisdom of the Perl Monks concerning the following question:
Is it possible to do the equivalent of an AUTOLOAD, but for accesses to undefined (un-existing) variables, not functions?
The basic scenario is that I'm maintaining some code that uses Perl as the format of a configuration file, and some people's config files access variables without pre-declaring them. There's a performance problem with the overlying script, becasue its taking a long time to calculate/fetch the values of all the variables that could be accessed in this config file, even though only a small number of variables are used in a given config file. We'd like to avoid modifying user-environments, and fix the performance by tweaking the master-script.
The current code that reads a config-file simply does:
So what I'd really like to do is to trap accesses to non-existing (scalar) variables in package "ConfigFile", and substitute a value that I determine using some expensive function call. E.g.
Unfortunately, I can't work out how to tie a symbol table (nor even if I really want to)
One option I've considered is to simply tie every known variable that anyone might want to access, and thus defer fetching the actual value until later. But this feels very, um, un-lazy. (And even working out what variables could be accessed can be somewhat expensive).
If there's no better way to do it, then I guess I'll have to bite the bullet and find an intrusive solution; and then get everyone to change their config files. But doing that with hundreds of users is not something I look forward to.
Opinions my own; statements of fact may be in error.