Module:No globals

From YSTV History Wiki
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 		-- perf optimization here and below: do not load Module:TNT unless there is an error
 5 		error(require('Module:TNT').format('I18n/No globals', 'err-read', tostring(k)), 2)
 6 	end
 7 	return nil
 8 end
 9 function mt.__newindex(t, k, v)
10 	if k ~= 'arg' then
11 		error(require('Module:TNT').format('I18n/No globals', 'err-write', tostring(k)), 2)
12 	end
13 	rawset(t, k, v)
14 end
15 setmetatable(_G, mt)