lua-users home
lua-l archive

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


Matthew Wild <mwild1@gmail.com> wrote:

> 2009/11/23 spir <denis.spir@free.fr>:
> > Hello,
> >
> > I'm looking for a straightforward way to define what a module exports -- other than setting all the rest global. Ideally, this would be something like:
> >
> > -- module mod
> > require("mod_test")
> > __export = {CONST=CONST, config=start_config, test=mod_test.test, do=safe_do}
> >
> > CONST = 3.14
> > start_config = {...}
> > do = function(...) ... end
> > safe_do = function(...) ... do(...) ... end
> >
> > Then, as you guess, requiring "mod" would export only the content of __export.
> > Is there method analog to this?
> >
> 
> At the bottom of the file put "return __export", it will be used as
> the return value from require, and saved in package.loaded[modulename]
> for future calls.

Great! That's exactly what I was looking for. (Didn't think, though, it could be _such_ straightforward).
By the way, does this means that require (or behing loadfile, or even load) will regard the whole module like a func?  maybe taking the module's global namespace as return value if none explicitely defined?

> Matthew
> 




________________________________

la vita e estrany

http://spir.wikidot.com/