Re: Confusion about environments in Lua 5.2
[
Date Prev][
Date Next][
Thread Prev][
Thread Next]
[
Date Index]
[
Thread Index]
- Subject: Re: Confusion about environments in Lua 5.2
- From: Philipp Janda <siffiejoe@...>
- Date: 2014年4月09日 06:26:19 +0200
Am 09.04.2014 06:13 schröbte Milind Gupta:
I did the following:
print(_G)
table: 00C31B98
print(_ENV)
table: 00C31B98
print(_G._G)
table: 00C31B98
print(_ENV._G)
table: 00C31B98
print(_ENV._ENV)
nil
print(_G._ENV)
nil
`_ENV` is an upvalue not a global. So it isn't in the globals table
referenced by `_ENV` or `_G` (which really is `_ENV._G`).
when I do print(_ENV) doesn't it access the variable _ENV._ENV? but
printing _ENV._ENV is nil. So does it mean when I access any other variable
aexcept _ENV it is accessed as _ENV.var?
No. The rule is simple. When you access a variable `x` you get the
innermost local or upvalue with that name in scope, or as a last resort
the global variable `_ENV.x` if there is none in scope.
So this means that a running code cannot change its own environment as it
could do in Lua 5.1.
Sure it can:
_ENV = {}
Milind
Philipp