lua-users home
lua-l archive

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


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'):


Then in your main program you:

  require "fruit"

and in fruit/init.lua you:


  greengrocer = true  -- package-visible variables

  require "fruit.banana"
  require ""

Inside the submodules you can do something like:

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

just before you do your:


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



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, 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.

Hakki Dogusan