lua-users home
lua-l archive

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

2011/3/31 Anthony Howe <>:
> I have a related problem with function environments in 5.1. How to get
> function environments to inherit the environment of the caller and NOT
> that of where it was defined.

Inside your function, you can get the environment of the caller with
getfenv, by passing it a number. You can then replace the current
function environment with that. For example :

function foo()
  setfenv(1, getfenv(2))
  -- from now on, all globals are accessing the caller's environment