lua-users home
lua-l archive

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


Tomas Guisasola Gorham wrote:
_G = { a = 4 }
print(a) --> 3  [*]
print(_G.a) --> 4

[*] with _ENV instead of _G, this would be 4 (if I'm not mistaken).
>
    In fact, it will throw an error, because print (or _ENV.print)
does not exist :-)

Right :-) I should have used { a = 4, print = print }

In short, _G is just a reference to the globals table; changing _G will not change the global table. On the other hand, _ENV is an upvalue containing the current 'globals' table; changing _ENV will change the 'globals'.
    Not exactly:

_G.print = nil -- equivalent to _ENV.print = nil

Ah, yes: I meant assigning _G itself, not its fields. My point was that _G references the globals table but the VM keeps its own reference to it (whatever happens to _G), while assigning _ENV actually changes the globals table used by the VM just like setfenv does.

  Enrico