lua-users home
lua-l archive

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


David Manura <dm.lua@math2.org> writes:
> -- load.lua
> local env; env = setmetatable({
>   import = function(name, value) env[name] = value end
> }, {__index = _G})
> assert(loadfile('foo.lua', nil, env))()

Hmmm, that would work, but would require one to keep tight control on
file loading so that the definition of 'import' stays in sync.  Maybe
that's a reasonable restriction, but I'm not sure...

[The explicit-environment version, although very slightly more
annoying to use, has the the advantage that it works basically
anywhere, and by being explicit, might have additional uses, e.g.,
"importing" directly into an explicit module table.]

-Miles

-- 
Friendship, n. A ship big enough to carry two in fair weather, but only one
in foul.