lua-users home
lua-l archive

Re: Package question

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


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.

AltStyle によって変換されたページ (->オリジナル) /