lua-users home
lua-l archive

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


On 27.02.2011 21:40, Sebastien Lai wrote:
> Recently I've stumbled upon an annoying, if not strange behaviour
> regarding the behaviour of 'require()'.
> In short, it seems that require() only searches from the directory from
> the script that is being called, for example:
> 
> 
> /path/to/somedir/main.lua
> |--> /path/to/somedir/system/
> |----> /path/to/somedir/lib/foo.lua
> |----> /path/to/somedir/lib/stuff.so
> 
> Main.lua being:
>     package.path = package.path .. ";../system/lib/?.lua"
>     require("foo")
> 
> Foo.lua being:
>     require("stuff")
> 
> In this case, the VM will search for "/path/to/somedir/stuff.lua", or
> "/path/to/somedir/stuff.so", but not for
> "/path/to/somedir/system/lib/stuff.so" or
> "/path/to/somedir/system/lib/stuff.lua", as for example Python does.
> 
> That is, Lua does not search the relative path, which makes using
> organized module directories rather tiresome to use...
> It wouldn't be all that terrible if one wouldn't have to explicitly add
> the following to "main.lua":
> 
>      package.cpath = package.cpath .. ";../system/lib/?.so"

It works for me as described (after adding package.cpath). Is your
foo.lua in somedir/lib/foo.lua or somedir/system/lib/foo.lua? Your
example is not consistent, and this may be the reason it is not working
for you.
BTW, you are mixing capitals (Foo.lua vs foo.lua) - this may work on
windows by accident, but is not portalble.

> A wake mind will see the problem here: the line has to be modified to
> match the extension for shared libraries on each system.
> I simply do not believe that this is intended, and writing a
> crossplatform version of 'dirname' is trivial, so why is this
> functionality not part of Lua yet?

That is the point: if it is so trivial it don't need to be added by
default (to the core lua). Nothing stops you from building your own
framework. Imagine there are tons of functions which are trivial to
write - should each of them be part of Lua by default?

Regards,
miko