lua-users home
lua-l archive

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

Peter Odding wrote:
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?

After posting the previous message I came up with the following solution which is actually quite straightforward, it just took me a while to get used to the new "in t do ... end" statement. Sorry for the noise :-)

-- $HOME/lua-5.2.0-work1/test.lua
package.path = '/home/peter/lua-5.2.0-work1/modules/?.lua'
require 'test'
assert( == 42)
assert(test.print == nil)

-- $HOME/lua-5.2.0-work1/modules/test.lua
local setmetatable, _G = setmetatable, _G
local mobtbl = module(...)
in setmetatable({}, {
	__index = function(_, k) return mobtbl[k] or _G[k] end,
	__newindex = function(_, k, v) mobtbl[k] = v end,
}) do
	function fun()
		print 'it works'
		return 42

 - Peter Odding