lua-users home
lua-l archive

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




On 15/10/16 03:35 AM, Sean Conner wrote:
It was thus said that the Great Egor Skriptunoff once stated:
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.
   This won't work with Lua modules written in C.  There was a thread about
this three years ago:

	http://lua-users.org/lists/lua-l/2013-06/threads.html#00464

But does C count as Lua?

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).
   First of all, all this will do is end up lengthening package.path with
repetitious paths already in package.path unless you take pains to write
code to check if the path already exists in package.path.

   Second, Lua has no concept of "directories" (or "folders" for that
matter).  All Lua does (using the second, third and fourth default
searchers; loaders in Lua 5.1) is replace the module name in a pattern and
attempt to open a file with the resulting name.

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)
   Third, this still leaves the first searcher, which simply scans
package.preload[] for the given module name, so if you are trying to load
"custom.sub1.foo", then that will attemp to look for

	package.preload['custom.sub1.foo']

   Then there are the custom loaders supplied by the user.  I have one at
work that will look for a module (written in Lua) internally (it's
compressed and compiled into the executable [1])---no concept of a
"directory" or "folder" there at all.

   -spc (So how is this supposed to work again?)

[1]	I go over a bit of the details here:
	http://boston.conman.org/2013/03/23.1


I made the OP because of Luvit ( https://luvit.io/ ). The people trying to overcomplicate 'require' don't understand what's going on with Luvit right now.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.