lua-users home
lua-l archive

Re: require ignores setglobals()

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


Hi,
> It did. Now you access the global table _G directly! So Lua4
> old = setglobals(sb) ; blah ; setglobals(old)
>
> becomes in Lua5
> old,_G=_G,sb ; blah ; _G=old
Unfortunately... not exactly. Functions keep their own reference to their
globals table and changing _G has no effect in the situation at hand:
function zoo()
 a = "aap"
end
sb = {}
old, _G = _G, sb
require() -- call earlier defined function
b = "noot" -- create new global
_G = old
print(a) -- prints "aap"
print(_G.a) -- prints "aap"
print(sb.a) -- prints nil [!]
print(b) -- prints "noot"
print(_G.b) -- prints "noot"
print(sb.b) -- prints nil [!!]
Keep in mind that the script itself _also_ has its own reference to a
globals table!!
Bye,
Wim

AltStyle によって変換されたページ (->オリジナル) /