lua-users home
lua-l archive

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


I have a simple example below.  It defines global a.  Changes the global
environment to a new empty table then defines global b. When I call
Blah() the new global b var is not defined. I understand that Blah() is
in the old global table but why doesn't it inherit the environment from
the calling function?
 
Thanks,
 
Jamie
 
 
-- Test file start -------------------------------------
function Blah()
 
 print("Blah()")
 print("a: " .. a)
 print("b: " .. b)
 
end
 
 
 
 a = 1
 
 local newgt = {}
 setmetatable(newgt, {__index = _G})
 setfenv(1, newgt)
 
 b = 2
 
 print("a: " .. a)
 print("b: " .. b)
 

 Blah()
-- Test file end ---------------------------------------
 

Output:
 
C:\Test>Lua Test.lua
a: 1
b: 2
Blah()
a: 1
Lua: Test.lua:7: attempt to concatenate global `b' (a nil value) stack
traceback:
        Test.lua:7: in function `Blah'
        Test.lua:25: in main chunk
        [C]: ?