lua-users home
lua-l archive

Re: require relative to file which is calling

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


2011年1月21日 Gustavo de Sá Carvalho Honorato <gustavohonorato@gmail.com>:
> how can I use require function passing the file path relative to the file
> that is calling the require function, as we do with #include in C?
Require doesn't work like C includes, its parameter is not a path but
a module name. The correct way to have it load a module that is
relative to the "requiring" file, depends on the type of file.
If the file that calls require is a script, it should configure
package.path. For example :
 package.path = package.path.."/path/to/your/script/?.lua"
If your file is another module, the file own module name is passed to
it as first parameter. From that you can find sub-modules. For example
:
 local modname = ...
 local submodule = require(modname..".subname")

AltStyle によって変換されたページ (->オリジナル) /