In this case the BEGIN basically causes the line initializing $output_dir to be executed before the sub that uses it gets called; without it, you'd have to rearrange the source code so that the initialization happened lexically before the first call to the sub. This is no big deal, at least in this case, but I like the freedom to arrange my code that the BEGIN trick allows. Combined with the enclosing block (around both the BEGIN block and the definition of process) it gives you something similar to (in fact better than) a C static variable. This is discussed at length here1.

1NB: in that thread I advocated using INIT blocks instead of BEGIN blocks, a position that was strongly challenged by ihb and ikegami. Since then I have learned that INIT blocks are broken, so I no longer recommend them.

the lowliest monk


In reply to Re^3: Hints for getting this perly... by tlm
in thread Hints for getting this perly... by hynek

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.