lua-users home
lua-l archive

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


On Mon, 8 Feb 2010 15:44:31 +0200
steve donovan <steve.j.donovan@gmail.com> wrote:

> On Sun, Feb 7, 2010 at 11:11 AM, spir <denis.spir@free.fr> wrote:
> > I guess the current dir (I mean ./) is not updated to /data when passing from the module "stack" to the module "STACK_CATS": when copying the file stack.lua into the top dir, all works fine. Am I doing something wrong? Is there a way to force module search in the actual current dir?
> 
> The problem often appears when wanting a program to be self-contained.

Exactly. I guess this is what I'm trying to do. But indeed I cannot know (before runtime) what the top dir of the program is. Instead, I know the program's internal hierarchy.

> Then the best way is for the main module to update package.path
> directly before requiring anything,

Thank you, Steve. I guess I will do that.

> but this requires that the module
> knows exactly where it is loaded from; you can usually get this from
> arg[0]. That is, say arg[0] is '/bonzo/dog/main.lua' then you can
> prepend '/bonzo/dog/?.lua;' to the package.path.

Yes. But for any reason arg[0] only tells me the module name without its path:
-- /home/spir/prog/lua/test/x.lua
print (arg[0])			--> x.lua
I can get the dir using
   print(os.getenv("PWD"))	--> /home/spir/prog/lua/test
on my computer, under linux, but this is indeed not portable.

Just to be sure I correctly understand. I naively thought the following: in the first path pattern of the require trial sequence "./?.lua", "./" refers to the path of the current module. Instead, if a requires b requires c, it seems "./" still refers to a's dir when b is run and requires c. As a consequence, "require 'c'" will search for c as "dir_of_a/c.lua" instead of "dir_of_b/c.lua". Correct? If I copy c into dir_of_a, all works fine.

Then 2 more questions:
* What about sub directories in a given package/app: I guess they must also be added to package.path?
* During development, for debug/setup/testing, I often need to run modules independently (so, there is neither import nore direct run of the main module): how to ensure proper update of package.path?
Maybe test funcs/modules should all include an update of package.path?

> steve d.




________________________________

la vita e estrany

http://spir.wikidot.com/