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:

sub read_config_file { my ($filename) = @_; package ConfigFile; do "$filename"; if (length $@) { ... } }
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.
tie %ConfigFile::, "ValueFetcher"
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.

--Dave
Opinions my own; statements of fact may be in error.

In reply to AUTOLOAD for variables? by dpuu

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.