gpt4 book ai didi

lua - lua 中有什么不同需要路径与 "/"或 "."

转载 作者:行者123 更新时间:2023-12-04 15:11:26 26 4
gpt4 key购买 nike

我看到一些 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
> ;-)

关于lua - lua 中有什么不同需要路径与 "/"或 ".",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65168192/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com