lua-users home
lua-l archive

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


Patrick Donnelly <batrick.donnelly <at> gmail.com> writes:
> When you call the module function, it changes the environment of the
> caller. In this case it's your redefined module function. Thus, when
> you define the function "hi" inside mymod.lua, it is actually placed
> in the global environment. If you are going to redefine the module
> function you are going to need to do a little more work:
> 
> local setfenv, getfenv = setfenv, getfenv
> 
> function module(modname, ...)
>   local myenv = getfenv(1)
>   original_module(modname, ...)
>   local newenv = getfenv(1)
>   setfenv(2, newenv)
>   setfenv(1, myenv)
>   -- do whatever else you wanted
> end

Patrick, thanks a lot, it works perfectly! I didn't manage to settle this 
getfenv/setfenv trick (but I suspected it would be possible to use them to 
solve this :-) ).
Thanks again,
Aladdin