lua-users home
lua-l archive

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



On 10/08/2007, at 6:13 PM, Tom Miles wrote:


Therefore whenever I wanted to require a package, I did:

utils = Require("utils")

This would then always directly reference the table package.loaded.utils

However, this didn't work as I expected in some cases.

I'm not sure if this helps, but when writing an application recently I found that if I did this:

a: require "b"
b: require "c"

(In other words, a indirectly acquires c)

... that c didn't have b's context. I wrote this loader for loading the submodules, which is a bit different to yours:

-- loader for modules to load sub-modules, in the module context
function my_loader (name)
  setfenv (assert (loadfile (
           string.format (MY_LUA_DIRECTORY .. "%s.lua", name))), _M) ()
end -- my_loader

my_loader "c"
my_loader "d"


This effectively lets me split my project into smaller pieces, however sub-modules (like c and d) did not have the module function in them, because they were really parts of module b, broken into smaller and more manageable pieces.

Without the setfenv above, doing dofile did not load c into the b's module context in the way I expected. Probably I should have researched it more. :)

- Nick