lua-users home
lua-l archive

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


Some time ago I've implemented sort of relative require() for my
own code, named request(). It operates on relative paths, converts
them to absolute paths for require() and calls it.

My need was to load code from sibling directory, for example
in deep_path/a/code.lua I wish to load code from deep_path/b/code.lua.
It may be done as request('^.b.code'). Limitation is that this call
only works when we have somewhere current path prefix, which is true
at first loading module.

So in main code you type request('deep_path.a.code').

This stores prefix 'deep_path.a' and calls require('deep_path.a.code').

request() in a/code.lua then knows current prefix and unites it
with '^.b', so current prefix becomes 'deep_path.b'.
And then calls require('deep_path.b.code').

You may see implementation in
https://github.com/martin-eden/workshop/blob/master/base.lua