lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Thu, Oct 13, 2016 at 10:28 PM, Soni L. <fakedme@gmail.com> wrote:

The proper way to do relative requires is:

thismod/init.lua
-----
local modname = ...
local submod = require(modname .. ".submod")
return {submod=submod}
-----

thismod/submod.lua
-----
return {
  helloworld = function() print("Hello World!") end
}
-----

Thanks, it is quite useful.

BTW, there is an idea how to improve require-related logic
require() should be rewritten so that each call to require() would add current module's directory to the beginning of package.path.
This "extended" package.path will affect only nested invocations of require().
Actually, it would be a stack of directories (synchronized with stack of "require" invocations).

This way you can simply omit concatenation of module names:

thismod/init.lua
-----
local submod = require("submod")
return {submod=submod}
-----

The module "submod" should be searched in the following places in the following order:
1) thismod/submod.lua
2) thismod/submod/init.lua
3) all standard paths (replacing "?" with "submod" in original package.path)