Scribunto: An Introduction/Your first module
Now that you are set up with a working version of Scribunto, it is time for you to write your first Lua program. All this program will do is output the text "Hello, world!" Not so useful by itself, perhaps, but we have to start somewhere.
The debug console
editFirst, we will run the program in the debug console, a text entry box that you can use to try out Lua code. To find the debug console, enter any module page in "edit" mode. You should see it near the bottom of the page.[1] It looks like this:
Once you've found the debug console, click on the text entry box, type the following code, and press enter.
After you press enter, you should see the following appear below the text you wrote:
Congratulations! You have just written your first Lua program. Let's look at the code in more detail.
The equals sign (=
) at the start of the program is a special command used in the debug console that runs the code on the current line. It must be used at the beginning of a line, and it can only be used in the debug console. So in our program, the code = "Hello, world!"
means 'run the code "Hello, world!"
'.
The two double quotes (""
) tell Lua to make everything inside them behave as a block of text. Blocks of text like this are known as strings to programmers. If you don't include the double quotes, Lua thinks that you are trying to input a command, and gets confused.
Lua knows that you made a mistake somewhere, but doesn't know exactly what it was, so it guesses that you need a ")" somewhere (wrong in this case).
Note that the spacing doesn't matter. You can put as many spaces as you want between the equals sign and "Hello, world!"
Running the code on wiki pages
editAt this stage, you are probably wondering how we can get our program to display on a wiki page. To do this, things get slightly more complicated. The creators of Scribunto, in their infinite wisdom, have decided that Scribunto programs have to be structured in a certain way, and that certain Lua functions (such as the print
function) should not be available. This was done to increase the quality of code that Scribunto users will write. The theory goes that teaching people good habits is more important than the convenience of being able to use functions like print
. This means that what would be a very simple program to write in standard Lua becomes more involved in Scribunto. The code we need is this:
p = {}
function p.hello_world()
return "Hello, world!"
end
return p
To run this code, we need to make a new module page. We will use "Module:Hello", but you can call it whatever you want. To get to the new page, you can make a link to your module using the syntax [[Module:Hello]]
and then follow that link, or you can type "Module:Hello" into the URL in place of the page name. Some wikis, for example the English Wikipedia, will also give you a link to follow if you type "Module:Hello" into the search bar. Once you are at the module page, click on the "Edit" or "Create" tab at the top of the page to get the edit view. Then copy and paste the code above into the edit box, and save the page. Your module is now ready to be run.
You can run the code from any normal page on your wiki, for example your user page or a sandbox page. Edit the page, and type in the following text:
{{#invoke:Hello|hello_world}}
If you named your module something other than "Module:Hello", then replace the first "Hello" with the name of your module. Then save the page. The page should now display the text, "Hello, world!" If this worked, then give yourself a pat on the back. You have just written a Lua module and displayed it from wikitext; that's not something that every wiki editor can do.
There are quite a few concepts in the module code above that are new to us. Try not to worry about them too much. For now, it is enough that you know that this module code will produce "Hello, world!" on a wiki page. You will learn about all the other concepts as you progress further through this book.
Notes
edit- ↑ The debug console requires JavaScript to run. For most people JavaScript will be turned on by default, but if you have disabled it then you will not be able to use the debug console. If you have JavaScript enabled but the debug console is not visible, there may be a JavaScript conflict preventing it from loading. Please ask your wiki's technical support staff for help. For Wikimedia wikis, the English Wikipedia's technical village pump may be able to provide assistance.