lua-users home
lua-l archive

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


This may seem naive but I am teaching myself Lua and seem to be missing something important.  

Reading the discussion of _ENV (in 5.2) I got the impression that local variables were stored in the  _ENV of that function and upvalues to defined functions were therefore stored in a copy of the  _ENV of the function that did the definition .  I wrote the following test snippet to check this:

function f(x)
print'in f'
local fvar = x
print(_G, _ENV)
print(_G.fvar,_ENV.fvar)
function g()
print'in g'
local gvar = 888
print(_G, _ENV)
print(gvar, fvar)
print(_G.fvar,_ENV.fvar, _G.gvar,_ENV.gvar )
end
end

but this gives the result:

> f(1000)
in f
table: 0x100100e50 table: 0x100100e50
nil nil
> g()
in g
table: 0x100100e50 table: 0x100100e50
888 1000
nil nil nil nil

Obviously things do not work the way I imagined.  _ENV is always equal to _G and I don't understand where either local variables or upvalues live.  This despite the fact that g() clearly is getting the correct upvalue.

I feel that I will not make any progress until I understand this critical point.  Can anybody give some advice to a new student?
Thanks



Jose Torre-Bueno