Module:No globals

From WikiProjectMed
Jump to navigation Jump to search

This module causes an error if any nil global is read or if any global is written to, with the exception of arg. To use, add require('Module:No globals') to the top of the module using it. The arg variable is excluded because it is necessary for Scribunto's require function to work properly. (See the Scribunto source code here.)

See also


 1 local mt = getmetatable(_G) or {}
 2 function mt.__index (t, k)
 3 	if k ~= 'arg' then
 4 		error('Tried to read nil global ' .. tostring(k), 2)
 5 	end
 6 	return nil
 7 end
 8 function mt.__newindex(t, k, v)
 9 	if k ~= 'arg' then
10 		error('Tried to write global ' .. tostring(k), 2)
11 	end
12 	rawset(t, k, v)
13 end
14 setmetatable(_G, mt)