-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathex12.lua
30 lines (25 loc) · 1.03 KB
/
ex12.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
-- Example 12. Safely load a configuration file
--8<----------------------------------------------------------------------------
local f = io.open('config.lua', 'w')
f:write([[
--8<----------------------------------------------------------------------------
-- File "config.lua".
width = 600
height = 400
home_path = "/home/mitchell"
--8<----------------------------------------------------------------------------
]])
f:close()
--8<----------------------------------------------------------------------------
-- Program code.
local config = {}
assert(loadfile("config.lua", "t", config))()
for option, setting in pairs(config) do
--[[ process option and setting ]]
--8<--------------------------------------------------------------------------
print(option, setting)
--8<--------------------------------------------------------------------------
end
--8<----------------------------------------------------------------------------
os.remove('config.lua')
--8<----------------------------------------------------------------------------