Questions like File-scoped lexicals and eval scope? come up now and then, and usually dave_the_m ("Mr. Scoping") answers them. But once long ago he wrote up five simple rules that provide the framework for addressing such questions on perl5-porters; I've kept them bookmarked and periodically refer to them, but thought it might be nice to have them more accessible. Without further ado:

At all times Perl follows the following five basic rules:

1. Any *use* of a lexical variable is, at *compile* time, matched against the nearest *lexically* (not dynmically) matching 'my' declaration.

2. Each time a block with a 'my' declaration is entered, a new instance of that lexical is created, and each time the block is exited, that instance is discarded (of course, if something else holds a reference to it, then the actual thinbg itself continues to exist, it is just not accessible via the lexical name). The one proviso to this is that the first instance exists from the moment of creation of the sub or file it resides in, rather from entry into the block during first execution.

3. At the creation time of a sub that references an outer lexical, that sub captures the current instance of that lexical. If there is no currently valid instance, a warning is issued, and a new undef value is 'captured' instead. For named subs, creation time equals compilation time; for anonymous subs, creation time is later, when you execute the 'sub' bit.

4. For our purposes, a string eval is just a sub that is compiled once, executed once, then discarded.

5. When a sub has captured an instance, any mention of '$x' which is lexically contained within that sub, such as in an eval or nested anonymous sub, will see that captured instance rather than the outer one.

Okay, I've skipped a few subtleties, such as what constitutes a sub, and what happens after you undef a sub, but that's the jist of it.


In reply to Rules of Lexical Scoping by ysth

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.