lua-users home
lua-l archive

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


On 05.12.2011 23:44, KR wrote:

> It would be nice to be able to have some code 
> that allows me to "import" all the lua files in a given directory (xxx_imp 
> above) to avoid this manual editing.

How about:
import(_M, require "mylib.xxx_imp")
and then:
-- mylib/xxx_imp/init.lua
require "mylib.xxx_imp.a"
require "mylib.xxx_imp.b"

> I guess this cannot be done via plain lua as it likely requires a library like 
> lfs. 

You could use io.popen("ls "..DIR.."/*.lua") (or equivalent on Windows)

> Moreover I am concerned about how to figure out what directory to look 
> into. I always simply used require, but I clearly cannot rely on it here (I 
> don't know the lua file-names I would be importing) so I would need to figure 
> out a way to discover where to look for "mylib.xxx_imp" as a directory.

You may use env variables:
$ LUA_PATH=/opt/mylib/?.lua lua myprogram.lua

or:

$ export MYLIB=/opt/mylib; lua myprogram.lua

-- main.lua:
DIR=os.getenv("MYLIB")

Regards,
miko