需要名称中带有点的文件夹中的模块

Mar*_*ski 6 lua

我想从名称中带有点 (.) 的文件夹中获取文件:

"Folder.ai/test.lua"
Run Code Online (Sandbox Code Playgroud)

如果文件夹名称中没有点,我会使用:

require(Folder.test)
Run Code Online (Sandbox Code Playgroud)

当有点的时候我该怎么办?

Nif*_*fim 5

require使用加载器查找文件,您可以通过将函数插入到package.loaders.

您的自定义加载程序可能如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

资源: http: //lua-users.org/wiki/LuaModulesLoader