Wikipedia:Lua: Perbedaan antara revisi

Konten dihapus Konten ditambahkan
kTidak ada ringkasan suntingan
Baris 1:
{{inuse}}
{{Information page|WP:Lua}}
{{Namespaces}}
Baris 5 ⟶ 6:
 
[[Kode sumber]] Lua disimpan di halaman bernama modul (misalnya [[Modul:Pisang]]). Kode-kode tunggal ini dapat ''dipanggil'' <!--bantu terjemahkan: invoked--> (dengan perintah <code><nowiki>{{#invoke:}}</nowiki></code>) di halaman templat (contoh pada halaman [[Modul:Pisang/doc]] menggunakan kode <code><nowiki>{{#invoke:Pisang|hello}}</nowiki></code> untuk menghasilkan teks "Hello, world!").
 
== Menjalankan modul ==
 
Modul dapat dijalankan di halaman wiki normal menggunakan fungsi parser #invoke. Sintaks #invoke mirip dengan sintaks templat, tetapi terdapat beberapa perbedaan. Perbedaan yang paling penting adalah Anda memerlukan ''nama fungsi''. Sebuah fungsi adalah satu set perintah yang mengambil nilai input, memprosesnya, dan menghasilkan suatu nilai input.<ref>Anda juga dapat menggunakan lebih dari satu nilai keluaran, tapi bukan berarti bahwa fungsi yang melakukanya dapat diakses dari sebuah halaman wiki lokal.</ref> Sistemnya sangat mirip dengan templat: Anda memasukan argumen, kemudian modul akan memprosesnya, dan Anda akan mendapat hasil keluaran. Bagaimapun, Anda dapat mendefinisikan beberapa fungsi dalam satu modul, sedangkan templat, Anda hanya dapat menggunakan satu fungsi untuk satu halaman.
 
Tambahan lagi, Anda tidak dapat menjalankan modul secara langsung - anda hanya dapat menjalankan salah satu ''fungsi'' modul. Modul hanyalah tempat untuk menampung fungsi.<!--, 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|apples=3|bananas=4}}</nowiki></code> &rarr; {{#invoke:BananasArgs|count_fruit|apples=3|bananas=4}}
* <code><nowiki>{{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}</nowiki></code> &rarr; {{#invoke:BananasArgs|count_fruit|bananas=5|apples=2}}
 
Most modules will have a documentation page explaining what arguments can be used and what their effects will be.-->
 
== Lihat pula ==