lua-users home
lua-l archive

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


On Tue, Nov 29, 2011 at 1:33 AM, Miles Bader <miles@gnu.org> wrote:
>  * The only place I use "getfenv" is in the form "getfenv (2)", to
>    define functions that modify the caller's environment, like
>    "import (MODULE, SYM1, SYM2, ...").
>
>    I guess what I'll do here is change the definitions of these
>    functions to take the target environment table as an explicit
>    first parameter, for which which callers will almost always pass
>    _ENV; in Lua 5.1, _ENV will just be nil, so these functions can
>    be defined like:

-- load.lua
local env; env = setmetatable({
  import = function(name, value) env[name] = value end
}, {__index = _G})
assert(loadfile('foo.lua', nil, env))()

-- foo.lua
import('a', 123)
print(a) --> 123