lua-users home
lua-l archive

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


I've always considered package.seeall an ugly solution because it makes
module tables shadow the globals table and I'm pretty sure I'm not
the only one [1]. Therefor I've always used the following code:

module(..., function(_M)
  setfenv(3, setmetatable({}, {
    __index = function(_, k) return _M[k] or _G[k] end,
    __newindex = function(_, k, v) _M[k] = v end,
  }))
end)

Can anyone tell me whether there is a way to accomplish the goal of
separating the module environment from the module table in Lua 5.2.0
(work1) without using the debug library? The reason I (and others)
insist on not using the debug library is because it's labeled
`debug' and the reference manual explains the name very clearly:

You should exert care when using this library. The functions provided
here should be used exclusively for debugging and similar tasks, such
as profiling. Please resist the temptation to use them as a usual
programming tool: they can be very slow. Moreover, several of these
functions violate some assumptions about Lua code (e.g., that
variables local to a function cannot be accessed from outside or that
userdata metatables cannot be changed by Lua code) and therefore can
compromise otherwise secure code.

To further clarify my position, I wouldn't mind if the use of setfenv() was discouraged by moving it from the globals to a separate library like `reflect' or `introspect' (which the user would have to explicitly require() for all I care) but I would hate for setfenv() to end up in the debug library, where it's pretty much off limits to anything except hacks and debuggers.

 - Peter Odding

[1] http://lua-users.org/wiki/LuaModuleFunctionCritiqued