[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: how to read lua table present in file
- From: Sean Conner <sean@...>
- Date: Mon, 16 Oct 2017 02:59:01 -0400
It was thus said that the Great prashantkumar dhotre once stated:
> hi
> i am new to lua. i have a query related to table reading from file.
> i have a config file in lua table format:
> example:
> return {
> ["param1"] = {
> ["attribute"] = {
> ["myparam"] = 1,
> }
> },
> ["param2"] = 1
> }
>
> could you please let me know how do i read this file and access my config
> file parameters ?
> Thanks
For Lua 5.1 (you did not mention which version you are using), I use:
CONF =
{
getenv = getenv, -- we allow this function be be called in the conf file
-- put any other functions you deem "safe" to call from a config
-- file here as well ...
}
local configfile,err = loadfile("myconfigfile.conf")
if not configfile then
os.exit(1) -- or handle a missing config file
end
setfenv(configfile,CONF)
configfile()
print(CONF['param1']['attribute']['myparam'])
For Lua 5.2 and above, it changes slightly:
CONF =
{
getenv = getenv,
}
local configfile,err = loadfile("myconfigfile.conf","t",CONF)
if not configfile then
os.exit(1) -- or handle a missing config file
end
configfile()
print(CONF['param1']['attribute']['myparam'])
I allow some functions (like getenv()) to be called from a config file
because it's nice to allow something like:
basedir = getenv("HOME") .. "/datadir"
webdir = getenv("HOME") .. "/public_html"
in a config file, but hey, leave the CONF table empty if you want maximum
security.
-spc