How to include a configuration file for variables in Lua

There are some variables in my lua script that I would like to put in the settings.conf file so that I can easily change the variables without diving into the code.
In other languages, they use "include", but in Lua it seems different because it loads the module . I need to download a configuration file for some parameters.
Which command should I use for this?

+4
source share
2 answers

The easiest way to execute a Lua script from another script is to use dofilethat translates the file path:

dofile"myconfig.lua"

dofile "/usr/share/myapp/config.lua"

dofile , script. , , , pcall:

local ok,e = pcall(dofile,"myconfig.lua")
if not ok then
  -- handle error; e has the error message
end

, loadfile, :

local f,e = loadfile("myconfig.lua")
if f==nil then
  -- handle error; e has the error message
end
local ok,e = pcall(f)
if not ok then
  -- handle error; e has the error message
end
+3

- :

config.lua:

myconf = {
    param1 = "qwe";
    param2 = 7;
}

:

package.path = '*.lua;' .. package.path
require "config"
print("config param1 = " .. myconf.param1 .. "\n")

.

0

Source: https://habr.com/ru/post/1608315/


All Articles