lua-users home
lua-l archive

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

On Fri, Jun 25, 2010 at 8:06 AM, mos <> wrote:
> I should make a summary:
> require a  .lua file not use ".lua" because require treat  '.' as '\\'
> when require "a.lua"
> the lua will look for "a\lua.lua"
> so when use relative path "..\" like require "..\\a\\a" you will never get right path
> the lua will look for \\a\a.lua still in the same dir b
> is that a bug ?
> 1. It should handle "..", if the prefix is ".." not treat as \\\\
> 2. (suggestion) if the postfix  is ".lua" not treat '.' as '\\'
> and I wish dofile will solved this, but in the a.lua
> will also require another file like 'c'
> a
>   a.lua
>   c.lua
> b
>   b.lua
> when use dofile in b.lua like dofile("..\\a\\a.lua") will cause a error can not find c.lua
> So I hope solved it.

I think that require() is not intended to be used in the way you are
trying to use it. The module name passed in is not expected to include
details of where the module is to be found on the file system.
Instead, you should be simply passing the pure module name "a", "b" or
"c" - the assumption being that the 'package' module should have been
configured so that Lua knows where to look to find your modules, if
they are not in the usual places.

You could try adding all of the additional directories a module could
be found to the package.path string like this - in your main script,
before you use require() anywhere:

  package.path = package.path .. ';../a/?.lua;../b/?.lua'

This means that when you call require("FOO"), it will look for
../a/FOO.lua and ../b/FOO.lua if it does not find the module anywhere

Alternatively, you could explicitly add all the module-loading scripts
into the package.preload table at startup:

  package.preload["a"] = loadfile("../a/a.lua")
  package.preload["b"] = loadfile("../b/b.lua")
  package.preload["c"] = loadfile("../b/c.lua")

This means that when you call require("a") to first load the module,
it will actually be calling the function found at