lua-users home
lua-l archive

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

2017-02-06 3:14 GMT+02:00  <>:

> Not to be nitpicky, the originally posted code does not add the current
> main but rather the location of the last require() which keeps everything
> relative to it's current location independent of main() or the
> interpreters path.  I personally found it useful.

Sorry for not understanding that. If I did, my answer could have been
much more to the point.

The answer to "why not?" is "because Lua already has a feature very
much like this, but safer to use".

>From the Lua end, it looks like this:

require "mymodule"         -- result goes into package.loaded.mymodule
require "mymodule.sub"  -- result goes into package.loaded["mymodule.sub"]

>From the package end it looks like this:


I.e. "mymodule" is a directory, not a file.

`require "mymodule"` first tries "mymodule.lua", and next
"mymodule/init.lua". Look at the default package.path.

Lua has had this feature for a LONG time. It's widely used in large packages.
E.g. in current Penlight, the three most basic submodules are loaded when
you say `require "pl"`, but you can load others.