Skip to content

Instantly share code, notes, and snippets.

@stuartpb
Created June 13, 2011 18:32
Show Gist options
  • Save stuartpb/1023377 to your computer and use it in GitHub Desktop.
Save stuartpb/1023377 to your computer and use it in GitHub Desktop.
Classic style 5.2 environmental module function
--has the same signature as classic module (almost)
local function module(name, encenv)
local mod_t = {}
local envmt = {}
function envmt.__index(t, k)
return mod_t[k] or encenv[k]
end
function envmt.__newindex(t, k, v)
mod_t[k] = v
end
package.loaded[name] = mod_t
return envmt
end
--this is the line that would go at the top of a module
local _ENV = module(..., _ENV)
-- all environment variables created here go into the module --
-- all environment variables read here read from the module --
-- if not present, they read from the encapsulating env --
-- the returned module does not provide access to the environment --
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment