Module:No globals

维基新闻,自由的新闻源

本模块将读取空全域变量和写入全局变量视为错误。要使用本模块,在模块上方添加

require('Module:No globals')

即可。排除arg变量是因为让 Scribunto require功能正确运行是必要的。(在此参阅 Scribunto 代码。)


-- 本頁面複製自[[w:zh:Module:No globals]],由機器人定期更新。請直接編輯原維基項目頁面,或自設定頁面[[User:Cewbot/log/20211203/configuration]]去除本頁面之後再編輯。
local mt = getmetatable(_G) or {}
function mt.__index (t, k)
	if k ~= 'arg' then
		error('尝试读取空全局变量:' .. tostring(k), 2)
	end
	return nil
end
function mt.__newindex(t, k, v)
	if k ~= 'arg' then
		error('尝试写入全局变量:' .. tostring(k), 2)
	end
	rawset(t, k, v)
end
setmetatable(_G, mt)