lua-users home
lua-l archive

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




On Fri, Dec 10, 2010 at 11:16 AM, Geoff Leyland <geoff_leyland@fastmail.fm> wrote:
On 10/12/2010, at 4:07 PM, starwing wrote:

> Lua5.2 has a new _ENV mechanism, it's simpler than previous global mechanism. and we can write
> do
>     local _ENV = ...
>     -- codes
> end
>
> to set a septate env for code chunks.
>
> but at a most times, we need import standard functions into the new _ENV, e.g. print, pairs, etc. may we register all these std-function into a pre-defined table?
>
> may it named _PRIM or _BASE or _BUILTIN, and we can generate new env table easily.
>
> or we can put this table into stdlib library.

But wouldn't normal practice be to put any of those functions into locals?

local print = print
do
 local _ENV = {}
 print "hello"
end

Cheers,
Geoff

this is a good practice! i forgot this!
then, how could we add all (builtin) function into a new _ENV? there are some practice way to do it?