[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: setting the _ENV for require
- From: Matthias Kluwe <mkluwe@...>
- Date: Wed, 07 Jun 2017 20:23:03 +0200
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