[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Accessing supermodule with require
- From: Duncan Cross <duncan.cross@...>
- Date: Fri, 23 May 2014 11:38:41 +0100
On Fri, May 23, 2014 at 11:25 AM, Minh Ngo <mkngo87@gmail.com> wrote:
> Is there an easy way to do the following without making a global variable?
You could explicitly set package.loaded[current_module_name] in each
module script, before ever calling require():
> @/foo.lua
> local foo = {}
package.loaded["foo"] = foo
> foo.bar = require("foo.bar")
> return foo
>
> @/foo/bar/init.lua
> local bar = {}
package.loaded["foo.bar"] = bar
bar.foo = require("foo")
> return bar
...it's a bit messy and you do have to be a bit more careful in the
main body of each of these scripts (since the require'd modules will
just be empty tables at the time of execution) but it should work.
-Duncan