lua-users home
lua-l archive

Re: Finding and loading Lua files

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


Since loadlib is already there, I don't think it would hurt a lot if
there was a requirelib function which would do for loadlib the same that
require does for loadfile (i.e. use LUA_PATH to search for the module).
At least for the symmetry. Or maybe even have require do all the work.
Here's the function I'm using now:
public.loadModule = function( module, importFunc, forceReload )
 if _LOADED[module] then
 if not forceReload then
 return _LOADED[module]
 else
 _LOADED[module] = nil
 end
 end
 if not importFunc then -- try a .lua module first
 local res, mod = pcall( require, module )
 if res then return mod end
 -- a .lua module was not found, try a DLL with luaLM_import
 importFunc = "luaLM_import"
 end
 local path = LUA_PATH or os.getenv "LUA_PATH"
 for m in string.gfind( path, "[^;]+" ) do
 m = string.gsub( m, "%?", module ) -- replace ? with module
 m = loadlib( m, importFunc )
 if m then
 m = m()
 _LOADED[module] = m
 return m
 end
 end
 return nil
end
Nothing OS-dependent here (except loadlib, which is already in the Lua
core). Now all I have to do is set the environment variable LUA_PATH to
?;?.lua;\path\to\LuaTools\?.lua;\path\to\LuaTools\?.dll
and then in the beginning of aTool\aTool.lua:
local utils = require "utils"
local subModule = utils.loadModule( "aTool/subModule" )
This would load both a Lua module from aTool\subModule.lua, or a DLL
module from aTool\subModule.dll
Ivan
Jamie Webb wrote:
On Friday 23 April 2004 11:28, Ivan Kolev wrote:
Something definitely must be added to Lua or the auxiliary libraries to
ease module finding and loading...
That sort of stuff is rather OS and application dependent. The core Lua distribution is intended to be as agnostic as possible. The sort of thing you're looking for is implemented by standalone distributions such as LuaCheia.
-- Jamie Webb

AltStyle によって変換されたページ (->オリジナル) /