Module:No globals
Lua
Code​Discussion​Edit​History​Links​Link count​Subpages:​Documentation​Tests​Results​Sandbox​Live codeAll modules This template is being used in the wrong namespace. Use {{Heavily used module}} instead!
This module is rated as ready for general use. It has reached a mature form and is thought to be bug-free and ready for use wherever appropriate. To reduce server load and bad output, it should be improved by sandbox testing rather than repeated trial-and-error editing.
This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing.
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.)
Code
local mt = getmetatable(_G) or {}function mt.__index (t, k) if k ~= 'arg' then error('Tried to read nil global ' .. tostring(k), 2) end return nilendfunction mt.__newindex(t, k, v) if k ~= 'arg' then error('Tried to write global ' .. tostring(k), 2) end rawset(t, k, v​)​end​setmetatable​(​_G​, mt)
Last edited on 19 February 2021, at 00:52
Wikimedia Commons
Files are available under licenses specified on their description page. All structured data from the file namespace is available under the Creative Commons CC0 License; all unstructured text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Terms of Use and the Privacy Policy.
Privacy policy
Terms of Use
Desktop
HomeRandom Nearby Log in Settings DonateAbout Wikimedia CommonsDisclaimers
LanguageWatchEdit