Created
July 20, 2011 13:20
-
-
Save gvx/1094934 to your computer and use it in GitHub Desktop.
Pythonesk "from ... import" in Lua, copying parts of a library into the global table
This file contains 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
-- CC0, see http://creativecommons.org/publicdomain/zero/1.0/ etcetera, etcetera | |
function from(source, which) | |
local t = require(source) | |
local function import (which) | |
if which == '*' then | |
for k, v in pairs(t) do | |
if _G[k] == nil then | |
_G[k] = v | |
end | |
end | |
else | |
for i, v in ipairs(which) do | |
_G[v] = t[v] | |
end | |
end | |
end | |
if which then | |
return import(which) | |
else | |
return import | |
end | |
end | |
--[[ usage: | |
from 'lib' '*' | |
from 'lib2' {'ball', 'rectangle', 'close'} | |
from ('lib3', '*') | |
]] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment