That's pretty simple. The perl compiler phase is what translates your sorce to an internal representation that gets executed--if it lost track of your variables as it did that it'd be a pretty poor compiler. ;)

Seriously, variables are in two places, as folks have more or less already pointed out. First are the global variables, the ones you can look up by name. They're stored in the %main:: hash, or a hash hanging off of it. Perl's got a handle on the %main:: hash stuck away in the interpreter structure, and can find it whenever it wants.

Subs have a scratchpad associated with them, where the lexicals are stored. A pointer to this structure is stored in the subroutine's internal bits, and when you enter a sub the interpreter instantiates the scratchpad for the sub.

Finding lexicals is actually easy. The compiler knows what lexicals you're using, as they're compile time constant things. When it goes and builds a sub's scratchpad it just stuffs all the unique lexicals for the sub into it. Since it keeps track of which lexical's gone in which slot, when it generates the "fetch a lexical" code for the interpreter, it just fills in which lexical slot the variable needs to come from.

There's nothing particularly revolutionary, or even cutting-edge, about perl's compiler code. You might want to find a book on the fundamentals of compilers (but not the Dragon book! It sucks) and read through it for a good idea of how this stuff all works.

Update: File-scoped lexicals are, interestingly enough, not a special case. Somewhere in the docs there's an off-hand comment about the entire file being wrapped in a set of braces. Well, as far as the compiler's concerned that's true--the whole file is, if there are any file-scoped lexicals, living inside an anonymous subroutine. (Which makes all the subs in a file with file scoped lexicals really a closure) Makes everything work out nicely.

If anyone's really interested in the way things are stored internally, I'd recommend starting in gv.c and going from there. Otherwise just assume it's all magic, which is as good an explanation as any.


In reply to Re: Perl Internals - references and symbol table by Elian
in thread Perl Internals - references and symbol table by nothingmuch

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.