dpuu has asked for the wisdom of the Perl Monks concerning the following question:
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.sub read_config_file { my ($filename) = @_; package ConfigFile; do "$filename"; if (length $@) { ... } }
Unfortunately, I can't work out how to tie a symbol table (nor even if I really want to)tie %ConfigFile::, "ValueFetcher"
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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: AUTOLOAD for variables?
by tachyon (Chancellor) on Jul 08, 2004 at 03:22 UTC | |
by dpuu (Chaplain) on Jul 08, 2004 at 04:19 UTC | |
by tachyon (Chancellor) on Jul 08, 2004 at 04:58 UTC | |
Re: AUTOLOAD for variables?
by Chmrr (Vicar) on Jul 08, 2004 at 04:48 UTC | |
by dpuu (Chaplain) on Jul 08, 2004 at 06:00 UTC | |
by dragonchild (Archbishop) on Jul 08, 2004 at 13:22 UTC |