lua-users home
lua-l archive

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


On Wednesday 27 August 2003 02:52, D Burgess wrote:
> In Peter Shooks lua2html there is the following line of code
> Can anyone exaplain it?
>
>   setfenv(1, setmetatable(Pkg, {__index=_G}))

It's not documented, but setmetatable() returns the table you pass it, so what 
that statement essentially says is 'make the contents of table Pkg override 
the global environment within the current scope.'

E.g. the following code prints "1   2   3"

a = 1
b = 5
c = 7

function f()
    local Pkg = { b = 2 }
    local c = 3
    setfenv(1, setmetatable(Pkg, { __index = _G }))
    print(a, b,c )
end

f()

 -- Jamie Webb

Give a man fire and he will be warm for a day. Set a man on fire and
he will be warm for the rest of his life. - Terry Pratchett