Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Perl Internals - references and symbol table

by Elian (Parson)
on Nov 16, 2002 at 20:45 UTC ( [id://213446]=note: print w/replies, xml ) Need Help??


in reply to Perl Internals - references and symbol table

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.

  • Comment on Re: Perl Internals - references and symbol table

Replies are listed 'Best First'.
Re: Re: Perl Internals - references and symbol table
by Elian (Parson) on Nov 17, 2002 at 18:34 UTC
    If you're wondering how code like:
    sub bar { my $foo; { my $foo; } }
    can possibly have only one scratchpad, that's easy--perl knows the two $foo slots are different, and makes sure it gets the right one when it goes looking. (When lexicals are accessed they're all accessed by slot number, not by name, so the actual name is pretty irrelevant at runtime)

    The only place that this could get interesting is with string eval, but since perl also tracks the lines that a particular lexical is active for, it's not a problem.

Re: Re: Perl Internals - references and symbol table
by Louis_Wu (Chaplain) on Nov 16, 2002 at 21:14 UTC
    book on the fundamentals of compilers (but not the Dragon book! It sucks)

    Why? Some people think that it is the "classic compiler text", what are they missing? Is this another holy war I'm not aware of?

    Let me clarify that I'm not challenging you (You're writing Parrot for goodness sake.), but I like to understand differences.

      The Dragon book is a classic because it was the first book in the field of any significance, not because it's actually any good. (Consider it a classic compiler book in the same way that "Plan 9 from Outer Space" is a classic SciFi movie) Everything you need for a simple compiler is in there, but you'll get it out with far more pain and confusion than the subject warrants. Newer books in the field, such as Modern Compiler Design by Grune, Bal, Jacobs & Langendoen (just off the top of my bookshelf), are much better and far easier to read.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://213446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (4)
As of 2024-04-25 16:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found