The strict check has a compiletime component to it. BEGIN blocks are guaranteed to be executed prior to anything else in the script, but they are compiled alongside the rest of the script, as they are seen, as the script is compiled line by line. In your script, 'print' is seen and compiled before your BEGIN block, and though the BEGIN block is executed before your 'print', the deed is already done; the compiler with strictures turned on has noticed that you have referred to a variable that hasn't been seen/declared yet. Yes, BEGIN executes first, but it is seen by the compiler in sequential order as the script is parsed top to bottom.

Using the use vars pragma as I suggested solves your scoping issues, but it doesn't change the fact that at compiletime, with the use strict pragma in place, the compiler is seeing a variable getting used before it is seeing the variable being declared. That's where you need to change your order, or move the "our" to outside the BEGIN block.


Dave


In reply to Re^3: Confusion about BEGIN block by davido
in thread Confusion about BEGIN block by Sandy

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.