"వికీపీడియా:Lua" కూర్పుల మధ్య తేడాలు

add a section on running modules from wiki pages
(→‎Unit testing: Added note about ScribuntoUnit)
(add a section on running modules from wiki pages)
 
The Lua [[source code]] is stored in pages called modules (e.g., [[Module:Bananas]]). These individual modules are then invoked (by "<nowiki>{{#invoke:}}</nowiki>") on template pages (e.g., [[Module:Bananas/doc]] uses the code <nowiki>{{#invoke:Bananas|hello}}</nowiki> to print the text "Hello, world!").
 
== Running a module ==
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.<ref>You can also have multiple output values, but functions that do this are not normally meant to be accessed from wiki pages.</ref> 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.
 
The simplest way to run a module from a wiki page is like this:
 
&#123;{#invoke:''module name''|''function name''}}
 
For example, we can run [[Module:Bananas]] in this way, which has one function named "hello".
 
* <code><nowiki>{{#invoke:Bananas|hello}}</nowiki></code> &rarr; {{#invoke:Bananas|hello}}
 
=== Using arguments ===
 
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.
 
&#123;{#invoke:''module name''|''function name''|''first positional argument''|''second positional argument''|''named argument'' = ''value''}}
 
For example, in [[Module:BananasArgs]], the "hello" function greets different people depending on the first positional argument. It works like this:
* <code><nowiki>{{#invoke:BananasArgs|hello|Kate}}</nowiki></code> &rarr; {{#invoke:BananasArgs|hello|Kate}}
* <code><nowiki>{{#invoke:BananasArgs|hello|Fred}}</nowiki></code> &rarr; {{#invoke:BananasArgs|hello|Fred}}
 
BananasArgs also has a function named "count_fruit" which uses the named arguments <code>bananas</code> and <code>apples</code> to count the number of bananas and apples we have. It can be run like this:
* <code><nowiki>{{#invoke:BananasArgs|count_fruit|bananas=4|apples=3}}</nowiki></code> &rarr; {{#invoke:BananasArgs|count_fruit|bananas=4|apples=3}}
* <code><nowiki>{{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}</nowiki></code> &rarr; {{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}
 
== Example modules ==
* [[Module:Sandbox]] provides a pseudo-namespace for experimenting with Lua modules.
* [[WP:Lua requests]] &ndash; requests for Lua-based templates or tasks
 
== Notes ==
{{reflist}}
 
{{WikiProject Lua/header}}
"https://te.wikipedia.org/wiki/ప్రత్యేక:MobileDiff/1387566" నుండి వెలికితీశారు