Aly
Aly

Reputation: 16255

Lua: load modules betwen sub-packages

I have the following package structure

--main.lua
--module1.lua
--utils/
    |----a.lua
    |----b.lua
--data/
    |----c.lua
    |----d.lua

from module1.lua I know that I can easily call utils.a and data.d for example. And also that within utils/a.lua I can reference utils/b.lua by doing something like

--a.lua
local current_package = (...):match("(.-)[^%.]+$")
require(current_package .. 'b')

But how do I require utils.a from data.c?

Thanks

Upvotes: 0

Views: 182

Answers (1)

siffiejoe
siffiejoe

Reputation: 4271

You can use

local parent_package = (...):match( "^(.-)[^.]+%.[^.]+$" )
require( parent_package .. "utils.a" )

if you think that your given package structure might be part of a bigger package structure. Otherwise you should just use absolute module paths from anywhere:

require( "utils.a" )

Upvotes: 1

Related Questions