lua-users home
lua-l archive

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


Hi,

Duck wrote:

Sometimes I need to share a variable among
submodules inside the same package.

Surely, if you arrange your packages so that instead of a bunch of submodules at the same level in the "package tree," there is one master module with the submodules below that, then you can require the master module first in order to define your package-specific variables, before requiring the various submodules.

For example (assuming you have './?/init.lua' in your package.path along with the more traditional './?.lua'):

  fruit/init.lua
  fruit/banana.lua
  fruit/mango.lua

Then in your main program you:

  require "fruit"

and in fruit/init.lua you:

  module(...)

  greengrocer = true  -- package-visible variables

  require "fruit.banana"
  require "fruit.mango"

Inside the submodules you can do something like:

  local shared = package.loaded[(...):match("%w*")]

just before you do your:

  module(...)

and then your package-only variables will be available in the submodules as:

 shared.greengrocer

etc.



I tried your module structure, but it didn't worked on Ubuntu-7.10 (it is working on winxp). What could be the reason?

Lua 5.1.2  Copyright (C) 1994-2007 Lua.org, PUC-Rio
> package.path="?;./?.lua;./?/init.lua"
> require"app"
error loading module 'app' from file 'app':
        cannot read app: Is a directory
stack traceback:
        [C]: ?
        [C]: in function 'require'
        stdin:1: in main chunk
        [C]: ?
>

"app" directory and "init.lua" exist.


--
Regards,
Hakki Dogusan