lua-users home
lua-l archive

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

I think you should read up on the "require" statement and more specifically the LUA_PATH.

For example I have in my LUA_PATH my "project working base directory", all Lua modules I write are placed in sub directories (eg.: .../projectBaseDir/x/y/z.lua) and can be accessed with a require relative to this base directory (eg.: require"x.y.z").
This works without problems and is comparable to the Java way of organising code.

The only "problem" I see is that LUA_PATH holds different directory entries and it's easy to get confused when you don't follow the basic rules.  Or : some module you require may be found in the standard distribution, another may be found from your "project working base directory" and another may be found from your present working directory...  If you start moving things around or Lua can find a module with the same name in a place you didn't expect or... it's easy to mess things up for yourself.


On Sun, Feb 7, 2010 at 1:52 PM, spir <> wrote:
On Sun, 7 Feb 2010 13:23:24 +0100
spir <> wrote:

> On Sun, 7 Feb 2010 10:29:23 +0100
> Wim Langers <> wrote:

> > you should include the full path to "Cat" as you did with "data.STACK_CATS"
> > to the directory from where you started the program.
> I think you mean that Cat should be required from STACK_CATS using
>     require "data.Cat"
> Right, this will work because STACK_CATS itself happens to be required from the upper directory. What if this changes, or if STACK_CATS is needed by several modules (eg by a test module in a /test subdir). I mean the relative requiring of Cat by STACK_CATS should be independent on external factors.
> Also, I guess this is the same scheme as when a package is required, no? How does the package's main module require utilities? (If the same thing happens, utilities will be searched in package importer's directory. Or is my reasoning wrong?)

In fact, I really need 2 cases, if only to be able to run and test STACK_CATS on its own. So, I found a temporary solution using:
   -- STACK_CATS.lua
   if not pcall(require, "Cat") then require "data.Cat" end
Is this a common idiom?
(Indeed, the 'then' part will save me only if STACK_CATS is itself required from the immediately upper dir ;-) I still think a simple require should work as expected as long as the relative positions of STACK_CATS & Cat do not change.)


la vita e estrany