lua-users home
lua-l archive

local "module" pattern without module [was Re: [ANN] SLAXML - pure Lua, robust-ish, SAX-like streaming XML processor]

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


On Feb 17, 2013, at 10:15 PM, Miles Bader <miles@gnu.org> wrote:
> Gavin Kistner <phrogz@me.com> writes:
>> * Adds only a single `SLAXML` key to the environment; there is no spam
>> of utility functions polluting the global namespace.
> 
> It should not add _any_ keys to the global environment.
> 
>> ## Usage
>> require 'slaxml'
> 
> local slaxml = require 'slaxml'
Thank you for the suggestion. I've updated the library to use this pattern.
This works fine for this project where I only have two files. How would others suggest enforcing the same pattern for a different project that has many files all augmenting the same table?
For example, in LXSC[1] I currently have 10+ files like so:
 lxsc.lua
 lib/event.lua
 lib/scxml.lua
 lib/state.lua
 lib/...etc...
and I use this to build the common object like so:
 # lxsc.lua
 LXSC = { VERSION="0.3.1" }
 require 'lib/state'
 require 'lib/scxml'
 require 'lib/event'
 ...etc.
 # lib/event.lua
 LXSC.Event = { ... }
 # lib/scxml.lua
 LXSC.SCXML = { ... }
 
 # lib/state.lua
 LXSC.State = { ... }
How might I modify this small multiple of files to work together in a way that doesn't spam the global namespace?
I can't do something like:
 # lxsc.lua
 local LXSC = { VERSION="0.3.1" }
 require 'lib/state'
 return LXSC
 # lib/state.lua
 local LXSC = require 'lxsc'
 LXSC.State = { ... }
…because that causes a loop in the loader. Should I instead do this?
 LXSC = { VERSION="0.3.1" } # Spam the global for now
 require 'lib/state'
 require 'lib/scxml'
 require 'lib/event'
 local LXSC = LXSC
 _G.LXSC = nil # remove the global spam
 return LXSC
Your experienced advice is requested :)
[1] https://github.com/Phrogz/LXSC

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