Created
June 13, 2011 18:27
-
-
Save stuartpb/1023370 to your computer and use it in GitHub Desktop.
A nice environmental module style for 5.2
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
| --takes the environment to encapsulate | |
| local function modme(encenv) | |
| local retmod = {} | |
| local envmt = {} | |
| function envmt.__index(t, k) | |
| return retmod[k] or encenv[k] | |
| end | |
| function envmt.__newindex(t, k, v) | |
| retmod[k] = v | |
| end | |
| end | |
| local _ENV, retmod = modme(_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 -- | |
| return retmod |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment