lua-users home
lua-l archive

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


2011/10/16 Sebastien Lai <237482@googlemail.com>:
> On Sun, Oct 16, 2011 at 3:40 AM, David Manura <dm.lua@math2.org> wrote:
>> I believe it's preferred to utilize package.preload rather than
>> override require.
>
> I've been trying to insert package.preloade[<modulename>] = <etc> in
> different ways, but it did not quite work for me.

Here is a simple working example :

package.preload.a = function(name)
    return {
        _NAME = name,
        f = print,
    }
end

local b = require 'a'

assert(b.f==print)
assert(b._NAME=='a')
assert(_G.a==nil)

What may not be clear is that preload must not contain the modules
themselves, but rather loader functions (that is, the functions that
will actually create the module, for example the body of a module
file).