|No such thing as a small change|
Style Question on Closuresby jynx (Priest)
|on Jan 01, 2002 at 06:19 UTC||Need Help??|
jynx has asked for the
wisdom of the Perl Monks concerning the following question:
Recently while developing a script i thought i found a good use for closures. The script itself is a pretty simple log parsing script, and on the whole i didn't need to use a closure, but the chance to learn got hold of me and i decided to use one anyway :-) Simply put, i'm running through a loop and i want to initialize an array with certain settings every time through. The settings are actually user-dependant and read from a file. Since reading from a file many times is a slow-up on the rest of the code i only wanted to read it once.
Of course i wouldn't be posting unless there was a problem. It turns out that i can use simple scalars/arrays/hashes just fine, but a code ref doesn't get "parsed"1 correctly. The problem i ran into is that the closure must be "parsed" before any code that calls it can run or else strict will die with the message:
<READ MORE> My solution was a BEGIN block, but i'm wondering if that's the best solution. Anything that requires magic to work makes me wonder about my design. So with that, here's a code example with documentation (using perl5.6.0). Please tell me if there's a better solution.
Since i want to have the actual main code at the top of the file (for maintainability) and not the subroutines, this is what i resorted to. The only other way i could see to get it to "parse" this first is to have a subroutine return this code as a string, and then eval the string at the top of the file. That sounded too icky so i avoided it. Either way i have to document this well so later maintainers don't ponder for 3 days why i did this.
Also, i searched for related topics and came up with this but testing showed that mentioning $get_strings again didn't solve the problem, so i don't think it's related. And if there's something in merlyn's WebTechnique column about this i didn't catch it, and those were the only highly pertinent links i found on the matter (but i have been known to be blind :-)
Any help with style, suggestions, otherwise is greatly appreciated,
1 "Parsed" is in quotes here because that's almost but not quite what i mean when i say that. IIRC there's a lot more that goes on than just "parsing"...