Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

require

lua

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?

like image 735
ice j Avatar asked Oct 16 '25 12:10

ice j


1 Answers

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
> ;-)
like image 104
koyaanisqatsi Avatar answered Oct 18 '25 09:10

koyaanisqatsi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!