Created
June 13, 2011 18:32
-
-
Save stuartpb/1023377 to your computer and use it in GitHub Desktop.
Classic style 5.2 environmental module function
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --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