lua-users home
lua-l archive

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


Hi!

It's easy to "sandbox" a chunk with an environment. Let's say I have a
file

    -- mod.lua -----------
    return function()
        return a
    end
    ----------------------

Then loading it with a given environment makes it independent of
`_ENV`:

    local g = loadfile( 'mod.lua', 't', {
        a = 42
    } )()
    print( g() ) --> prints 42
    _ENV.a = 23
    print( g() ) --> prints 42

That's not the case when using `require`:

    local f = require 'mod'
    print( f() ) --> prints 23
    _ENV.a = 42
    print( f() ) --> prints 42

Is there some clever trick to achieve the same effect (meaning to make
the required module independent of `_ENV')?

Regards,
Matthias