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:

Can't use string ("") as a subroutine ref while "strict refs" in use a +t temp line 21.
<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.

#!/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
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,
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"...


In reply to Style Question on Closures by jynx

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.