[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: require cascade
- From: Jerome Vuarand <jerome.vuarand@...>
- Date: Mon, 8 Feb 2010 14:24:42 +0100
2010/2/7 spir <denis.spir@free.fr>:
> On Sun, 7 Feb 2010 10:29:23 +0100
> Wim Langers <wim.langers@adrias.biz> wrote:
>
> Thank you. Here is a dir tree to sum up the issue:
> /app
> stack.lua requires "data.STACK_CATS"
> /data
> STACK_CATS requires "Cat"
> Cat
> It seems the require cascade does not work because Lua looks for Cat inside /app instead of /app/data.
Module lookup don't use relative paths, due to the way modules are
loaded. There is no magic behind the manipulation of modules. The
content of the file is just fed into loadfile, the current directory
doesn't change in the process. One big property is that the data.Cat
module will be loaded the same way whether it's from stack.lua or the
data.STACK_CATS module. Its name will still be "data.Cat", and thus to
load it you call require("data.Cat").
>> 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.
If you use the "module" function, you can call
require(_PACKAGE.."Cat") in STACK_CATS so that you can move both
without modifying them.
> 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?)
>
>> Alternatively you can change your working directory (eg. with lfs) to the
>> "Cat" directory (which I don't think is such a very good idea)
>
> Yes, I guess this would ensure the relative requiring of Cat by STACK_CATS. But is this needed, and why then? If it's needed, why do you think it's not a good idea? (What else can I do?)
No one will argue that what you describe is not good. But you have to
show that it's better than the alternative if you want some impact.
> Actually, I wonder whether Lua apps or packages are simply supposed to hold in a single dir? (Could not find any doc on the topic of structuring them -- PiL itself does not address it.)
Using module and _PACKAGE fixes most of the issues you may have.