Subject namespaces | Talk namespaces | ||
---|---|---|---|
0 | (Main/Article) | Talk | 1 |
2 | User | User talk | 3 |
4 | 5 | ||
6 | File | File talk | 7 |
8 | 9 | ||
10 | Template | Template talk | 11 |
12 | Help | Help talk | 13 |
14 | Category | Category talk | 15 |
100 | Portal | Portal talk | 101 |
108 | Book | Book talk | 109 |
118 | Draft | Draft talk | 119 |
446 | Education Program | Education Program talk | 447 |
710 | TimedText talk | 711 | |
828 | Module | Module talk | 829 |
2300 | Gadget | Gadget talk | 2301 |
2302 | Gadget definition | Gadget definition talk | 2303 |
-1 | Special | ||
-2 | Media |
The Lua source code is stored in pages called modules (e.g., Module:Bananas). These individual modules are then invoked (by code {{#invoke:}}
) on template pages (e.g. Module:Bananas/doc uses the code {{#invoke:Bananas|hello}}
to print the text "Hello, world!").
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.
For example, we can run Module:Bananas in this way, which has one function named "hello".
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.
For example, in Module:BananasArgs, the "hello" function greets different people depending on the first positional argument. It works like this:
BananasArgs also has a function named "count_fruit" which uses the named arguments bananas
and apples
to count the number of bananas and apples we have. It can be run like this:
Most modules will have a documentation page explaining what arguments can be used and what their effects will be.
Lua is a scripting language which can be used to analyze data, calculate expressions, and format results using functions or object-oriented programming. Although some Lua scripts can be kept simple, for easy understanding, Lua allows complex structures including tables, dynamic functions, and associative arrays where index subscripts can be words as well as index numbers. Lua also supports recursion of re-nested functions, so care should be taken to avoid excessive complexity where other users would not understand how to maintain a Lua module. The following is an example of Lua source code for a hello world function contained in Module:HelloWorld: