lua 需要带有“/”或“.”的路径有什么不同

what's different in lua require path with "/" or "."

我看到一些 lua 代码以两种方式使用 require 路径。 例如:

require("a/b/c")
or
require("a.b.c")

所以我想知道上面有什么不同。 还有我们可以在程序中同时使用这两种方式吗?

简单的方法是在 Lua 控制台中执行此操作 lua -i - 所以...

>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 ?
>

请注意,这出现在 a.b.c 的输出中,而不是 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'

...这是否回答了您的问题?

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
> ;-)