lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


This is the other part that confused me:

   _ENV is the "global scope"

What does that mean? In the PiL^3 book, it says something similar, but
instead of _ENV = _G, it says _ENV = <global environment> which made
me think it was doing a special per-entry copy into a new table or
something.

So I guess it was how _ENV is populated that I was actually confused
about this whole time, and still am.

On Mon, Aug 11, 2014 at 12:37 PM, Coda Highland <chighland@gmail.com> wrote:
> On Mon, Aug 11, 2014 at 10:31 AM, Steven Degutis <sbdegutis@gmail.com> wrote:
>> I've also been pretty confused on what _G does specifically.
>
> Instead of trying to respond to the contents of the e-mails, I'll give
> a very short explanation.
>
> When a chunk (usually a file, but also anything you've created with
> something like loadstring) starts, _G is a reference to _ENV.
>
> (_ENV is the "global scope", and any variable references that can't be
> found as local definitions or upvalues are looked up in the _ENV
> table. You can explicitly look inside _ENV if you want to grab a
> global that was shadowed by a local or upvalue.)
>
> Beyond that, there's absolutely nothing magical about _G. Its purpose
> is to be a reference to the original _ENV if you replace it. All of
> the remaining commentary is just a description of how non-magical _G
> is.
>
> That's literally all there is to it.
>
> /s/ Adam
>