[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: require() relative to calling file
- From: Dirk Laurie <dirk.laurie@...>
- Date: Mon, 6 Feb 2017 06:23:29 +0200
2017-02-06 3:14 GMT+02:00 <tobias@justdreams.de>:
> 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:
mymodule
init.lua
sub.lua
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.
Regards
Dirk
- References:
- require() relative to calling file, tobias
- Re: require() relative to calling file, Sean Conner
- Re: require() relative to calling file, Russell Haley
- Re: require() relative to calling file, Scott Morgan
- Re: require() relative to calling file, Russell Haley
- Re: require() relative to calling file, Sean Conner
- Re: require() relative to calling file, Russell Haley
- Re: require() relative to calling file, Sean Conner
- Re: require() relative to calling file, Russell Haley
- Re: require() relative to calling file, Dirk Laurie
- Re: require() relative to calling file, tobias