|Just another Perl shrine|
Re: Variable "%data" is not availableby ikegami (Pope)
|on Mar 21, 2011 at 20:53 UTC||Need Help??|
It's covered by the second one. %data has gone out of scope. The lexical block in which it resides (the file) was exited before AUTOLOAD was called.
A sub can extend the life of a lexical by becoming a closure and capturing the lexical. However, a sub only captures the variables it needs, meaning it only captures the lexicals it references. Other lexicals aren't captured.
If there's a reference to (i.e. a mention of) %data in AUTOLOAD (e.g. if you uncomment any of the commented statements), then the sub captures %data, and the eval will find %data.
If there are no references to %data in AUTOLOAD, then the sub doesn't capture %data, so it won't exist when eval is evaluated.
Adding «%data if 0;» to AUTOLOAD is enough to make it capture the lexical without warnings.
Another option is to use a package variable since they don't go out of scope.
I cringe whenever someone does
because they're not equivalent. The crucial difference in this case is that you now call AUTOLOAD before the lexical scope of %data is exited.