lua-users home
lua-l archive

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


I wrote a searcher to basically search for modules with sub modules properly in my installation directory structure. But I do not see the error message returned by my searcher in the final result.

I have the following directory structure:

Structure Root
   |----  __Lua\Lua53\lua.exe    - lua executable and dll
   |----  moduleX
                |----  src  -- All module lua files
   |---- moduleY

So suppose the moduleX has a submodule test. And when I do require("moduleX.test") it basically needs to load the file -> "Structure Root/moduleX/src/moduleX/test.lua" I added the path in package.path as "Structure Root/?/src/?.lua". This of course does not work since the default searcher translates it to  "Structure Root/moduleX/test/src/moduleX/test.lua". So I wrote this searcher and added it to searchers:

-- Searcher for nested lua modules
package.searchers[#package.searchers + 1] = function(mod)
-- Check if this is a multi hierarchy module
if mod:find(".",1,true) then
-- Get the top most name 
local totErr = ""
local top = mod:sub(1,mod:find(".",1,true)-1)
local sep = package.config:match("(.-)%s")
local delim = package.config:match(".-%s+(.-)%s")
local subst = mod:gsub("%.",sep)
-- Now loop through all the lua module paths
for path in package.path:gmatch("(.-)"..delim) do
if path:sub(-5,-1) == "?.lua" then
path = path:sub(1,-6)..subst..".lua"
end
path = path:gsub("%?",top)
--print("Search at..."..path)
-- try loading this file
local f,err = loadfile(path)
if not f then
totErr = totErr.."no file '"..path.."'\n"
else
--print("FOUND")
return f
end
end
return nil,totErr
end
end

Now when I do require("moduleX.test") it is successfully able to load that module. But if I do require("moduleX.tst") I see no error message returned by my searcher. Can anybody please help me figure out what am I doing wrong here.

Thanks,
Milind




On Mon, Aug 10, 2015 at 1:35 PM, Milind Gupta <milind.gupta@gmail.com> wrote:
Thank you very much for the detailed explanation.

Milind


On Mon, Aug 10, 2015 at 12:59 PM, Philipp Janda <siffiejoe@gmx.net> wrote:
Am 10.08.2015 um 21:37 schröbte Milind Gupta:
Hi,

Hi!

      I am trying to understand how does the require function invoke
package.searchpath and package.searchers. Does a searcher called searchpath
function? If yes then that means the searcher is responsible for reading
the paths and deciding which one is a lua fie (for example the lua module
searcher) and then passing the paths one by one to package.searchpath?

`require` calls the functions in `package.searchers` one after the other passing the module name as an argument until one of the searchers returns a function (the loaded chunk) or raises an error (e.g. if the module was found but contained syntax errors). If the searchers return a string instead, those error messages are concatenated and printed at the end if all searchers were unsuccessful.

`require` does not call `package.searchpath` directly, but a searcher may of course. All standard searchers except the first (the `package.preload` searcher) use the equivalent C code.

`package.searchpath` takes a path template list (usually `package.path` or `package.cpath`) and a module name, and creates a list of actual paths. It then tries those paths one by one, searching for a file that exists (is readable). If it finds one, it returns the path to that file. Otherwise it returns `nil` plus a string that can be returned from a searcher as an error message.


Thanks,
Milind


Philipp