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.Can't use string ("") as a subroutine ref while "strict refs" in use a +t temp line 21.
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.#!/usr/bin/perl -w use strict; # To test this uncomment these lines and (un)comment BEGIN {} #my @strings = get_init_strings(); #print "strings: @strings\n"; BEGIN { { # begin closure my @strings; my $get_strings = sub { # Trivialized code here for brevity. # This actually reads in from a file with lots of error checking. return "this", "that", "the", "other"; }; # the actual subroutine, the only thing callable outside of the clos +ure sub get_init_strings { if (!@strings) { # only call $get_strings o +nce @strings = $get_strings->(); } return @strings; } } # close closure } # close BEGIN block
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,
jynx
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"...
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Style Question on Closures
by chip (Curate) on Jan 01, 2002 at 06:49 UTC | |
by jynx (Priest) on Jan 01, 2002 at 07:59 UTC | |
by jeffa (Bishop) on Jan 01, 2002 at 23:26 UTC | |
Re: Style Question on Closures
by Juerd (Abbot) on Jan 02, 2002 at 00:06 UTC |