lua-users home
lua-l archive

[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]
            _LOADED[module] = nil

    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"

    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
    return nil

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


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


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