The interleaving of the definition of 'global' variables (either lexical or package) with the invocation of functions that access them has yet another tricky aspect.

Variables are defined at compile time, but not initialized until run time (unless explicitly initialized in something like a  BEGIN or  INIT block or through some other compile-time mechanism such as the use built-in). This leads to situations in which a function can access a variable that has been created and exists (as proven by the fact that the code runs under strictures), but has no defined value. Invocation of the same function at a different point in the code will find the function accessing a defined value. All the more reason to be very circumspect about the use of global variables.

>perl -wMstrict -le "func('before'); my $tricky = 'initialized at runtime'; func('after'); ;; sub func { printf qq{$_[0]: %s defined: }, defined($tricky) ? 'is' : 'NOT'; print qq{'$tricky'}; } " Use of uninitialized value $tricky in concatenation (.) or string ... before: NOT defined: '' after: is defined: 'initialized at runtime'

In reply to Re: Scope of lexical variables in the main script by AnomalousMonk
in thread Scope of lexical variables in the main script by sophate

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.