Wikipedia:Lua


Lua is a programming language that is available via the Scribunto MediaWiki extension on the English Wikipedia. Since February 2013, Lua code can be embedded into wiki templates by employing the "{{#invoke:}}" functionality of Scribunto. This extension supports Lua 5.1 as of July 2015.

The Lua source code is stored in pages called modules (e.g., Module:Example). These individual modules are then invoked (by code {{#invoke:<Module name>|<Function name>|(optional) param1 | param2...}}). Example:

Modules are run on normal wiki pages using the #invoke parser function. The syntax of #invoke is similar to template syntax, but with some differences. The most important difference is that you need to specify a function name. A function is a set of instructions that takes input values, processes them, and returns an output value.[1] This is much like what a template does: you give it arguments, it processes them, and you get a result. However, you can define many functions in one Lua module, whereas you can only define one template on one page.

Furthermore, you can't just run a Lua module directly – you can only run one of the module's functions. The module is just a container for the functions, and doesn't do anything by itself. So there are two reasons that we need to input a function name: we can't run a module by itself, and without specifying a function name, Lua will not know which function it is we want to run.

Arguments are passed to modules in the same way that they are passed to templates. Note, however, that the text after the first pipe character is always the function name; the first positional argument is the text after the second pipe.

In Module:Example, the "hello_to" function greets different people depending on the first positional argument. It works like this: