I see some lua code using require path in two way. eg:
require("a/b/c")
or
require("a.b.c")
so i want to know what's different above. And can we use this two kinds way in programa same time?
The simple way is to do that in the Lua console lua -i
- So...
>lua -i
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> require("a/b/c")
stdin:1: module 'a/b/c' not found:
no field package.preload['a/b/c']
no file '/usr/local/share/lua/5.3/a/b/c.lua'
no file '/usr/local/share/lua/5.3/a/b/c/init.lua'
no file '/usr/local/lib/lua/5.3/a/b/c.lua'
no file '/usr/local/lib/lua/5.3/a/b/c/init.lua'
no file './a/b/c.lua'
no file './a/b/c/init.lua'
no file '/usr/local/lib/lua/5.3/a/b/c.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './a/b/c.so'
stack traceback:
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
> require("a.b.c")
stdin:1: module 'a.b.c' not found:
no field package.preload['a.b.c']
no file '/usr/local/share/lua/5.3/a/b/c.lua'
no file '/usr/local/share/lua/5.3/a/b/c/init.lua'
no file '/usr/local/lib/lua/5.3/a/b/c.lua'
no file '/usr/local/lib/lua/5.3/a/b/c/init.lua'
no file './a/b/c.lua'
no file './a/b/c/init.lua'
no file '/usr/local/lib/lua/5.3/a/b/c.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './a/b/c.so'
no file '/usr/local/lib/lua/5.3/a.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './a.so'
stack traceback:
[C]: in function 'require'
stdin:1: in main chunk
[C]: in ?
>
Note this appeared in the output with a.b.c
but not a/b/c
:
no file '/usr/local/lib/lua/5.3/a.so'
no file '/usr/local/lib/lua/5.3/loadall.so'
no file './a.so'
...does this answer your question?
Try this...
>lua -i
Lua 5.3.5 Copyright (C) 1994-2018 Lua.org, PUC-Rio
> dump=function(dump) for key,value in pairs(dump) do io.write(string.format("%s = %s\n",key,value)) end
>> end
> dump(package.preload)
> package.preload["a.b.c"]=function() return "WHATSUP" end
> dump(package.loaded)
package = table: 0x56614f00
io = table: 0x56615860
math = table: 0x56616ce0
os = table: 0x56615f40
coroutine = table: 0x566153a0
bit32 = table: 0x56618730
debug = table: 0x56618350
utf8 = table: 0x56617880
string = table: 0x566166b0
_G = table: 0x566136d0
table = table: 0x56615640
> require("a.b.c")
WHATSUP
> dump(package.loaded)
package = table: 0x56614f00
io = table: 0x56615860
math = table: 0x56616ce0
os = table: 0x56615f40
coroutine = table: 0x566153a0
a.b.c = WHATSUP
bit32 = table: 0x56618730
debug = table: 0x56618350
utf8 = table: 0x56617880
string = table: 0x566166b0
_G = table: 0x566136d0
table = table: 0x56615640
> ;-)