[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Re: does the current environment revert automatically?
- From: Roberto Ierusalimschy <roberto@...>
- Date: Wed, 19 Dec 2007 18:45:39 -0200
> On page 254 of the Blue PIL, there's a nice example of using
> a fresh environment to share information among functions in a library.
> One thing I'm not clear on: whose responsibility is it to restore the
> previous environment? The example doesn't show the environment
> being restored. Is it somehow restored magically? Does 'require'
> reset the environment when it's finished? Or is the example wrong,
> and is it up to me to restore the environment in my own code?
It is never restored. What was changed was the environment of the
running function, luaopen_foo (in its Lua incarnation). This function
opens the library (which inherits its environment) and returns. It will
keep its changed environment for ever (which means until being collected
as garbage, after returning to 'require').