Lua 需要名称中带有点的文件夹中的模块
我想从名称中包含点(.)的文件夹中获取文件: 如果文件夹名称中没有点,我将使用:Lua 需要名称中带有点的文件夹中的模块,lua,Lua,我想从名称中包含点(.)的文件夹中获取文件: 如果文件夹名称中没有点,我将使用: require(Folder.test) 当点存在时,我应该怎么做?需要使用加载程序查找文件,您可以通过将函数插入到包中来添加自定义加载程序。加载程序 您的自定义加载程序可能如下所示: local function load(modulename) local errmsg = "" for path in string.gmatch(package.path, "([^;]+)") do loc
require(Folder.test)
当点存在时,我应该怎么做?
需要
使用加载程序查找文件,您可以通过将函数插入到包中来添加自定义加载程序。加载程序
您的自定义加载程序可能如下所示:
local function load(modulename)
local errmsg = ""
for path in string.gmatch(package.path, "([^;]+)") do
local filename = string.gsub(path, "%?", modulename)
local file = io.open(filename, "rb")
if file then
-- Compile and return the module
return assert(loadstring(assert(file:read("*a")), filename))
end
errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
end
return errmsg
end
table.insert(package.loaders, 2, load) -- this will run before the standard loader, if you want it to
-- run after you can call table.insert(package.loaders, load)
资源:
我认为您可以添加自定义加载程序/搜索程序
local function load(modulename)
local errmsg = ""
for path in string.gmatch(package.path, "([^;]+)") do
local filename = string.gsub(path, "%?", modulename)
local file = io.open(filename, "rb")
if file then
-- Compile and return the module
return assert(loadstring(assert(file:read("*a")), filename))
end
errmsg = errmsg.."\n\tno file '"..filename.."' (checked with custom loader)"
end
return errmsg
end
table.insert(package.loaders, 2, load) -- this will run before the standard loader, if you want it to
-- run after you can call table.insert(package.loaders, load)