lua-users home
lua-l archive

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


On Fri, Feb 5, 2010 at 3:23 PM, spir <denis.spir@free.fr> wrote:
> Hello,
>
> What is the proper way to require another module in the same package, that is with a relative path? Typically, require a module in /app/tools from another in /app/lib.
>
> Tried
>     require "..tools.modname"
> but this get translated to
>     ".///tools/modname.lua"

Assuming you are using the module() function, you should be able to
get the value _PACKAGE, which in this case will be "app.lib.". You can
use string.match to strip this down to "app.", concatenate the
"tools.mymodule" part onto it, and pass the result of that into
require():

 --
 local require = require
 local strmatch = string.match

 module(...)

 _PACKAGE_PARENT = strmatch(_PACKAGE, "^(.-%.)[^%.]+%.$") or ''
 require( _PACKAGE_PARENT .. "tools.mymodule" )
 --

-Duncan