lua-users home
lua-l archive

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


----- Original Message -----
From: steve donovan
Date: 2/19/2010 12:36 AM
On Thu, Feb 18, 2010 at 11:48 PM, Geoff Leyland
<geoff_leyland@fastmail.fm>  wrote:
local env = {}
local f = assert (loadfile ("config.lua"))
setfenv (f, env) ()

... which lets you load a configuration file into a table, will no longer work under Lua 5.2.
I do exactly this too.  It'd be a shame if 5.2 broke it.
Do remember that loadin can also be passed a code string (like
loadstring) but then I guess you still have to have io.read().

Although surely this is not the kind of thing you would do in a sandbox?
Another way I use this pattern (which is all the time) is for merging multiple levels of config files:

UserConfig.lua:

Config = {
    -- Stuff
}


MasterConfig.lua:

Config = {
    -- Stuff
}


dofile("MasterConfig.lua")

local env = {}
local chunk = loadfile("UserConfig.lua")
setfenv(chunk, env)()

-- Merge env.Config into Config.

How do we do this efficiently under Lua 5.2?

Josh