User:RexxS/GCI-2019-Task02

Lua Task 2 - Working with modules
Prerequisite: Lua Task 1 - Introduction to Lua in Wikipedia

Creating your own Lua module
You can write your own Lua functions using your module sandbox to try out code. You can then use your user sandbox to call a function from your module sandbox and see how well it works.

1. Read the "Introduction - Getting started" and "Introduction - Module structure" sections at

mw:Extension:Scribunto/Lua reference manual

Make sure you understand that the module begins by defining an empty table, then defines functions (adding them to the table), and finally returns the table.

2. In your user sandbox (not your module sandbox), start a new level 2 section by typing  on a new line at the end of your sandbox.

3. Below that, copy the following three questions into your user sandbox:

Q1. What keyword marks the start of a function definition? A1. Q2. Which letter is conventionally used in Scribunto modules to hold the table of functions? A2. Q3. What keyword marks the end of a function definition? A3.

4. Write your answer to each of the three questions on the line below each question. Save your user sandbox.

5. In your module sandbox you should just have a comment ( -- YourUsername Google Code-in 2019, Introduction to Lua in Wikipedia ). On a new line write another comment with just the name of this task. Save your module sandbox.

6. Below that comment, copy all of the code below (which will create the "hello" function) into your module sandbox:

Make sure you include the initial line beginning " ", and the final line beginning " ". This example is taken from the example at mw:Extension:Scribunto/Lua reference manual.

7. Save your module sandbox.

8. Work out want you need to write in your user sandbox in order to call the "hello" function from your module sandbox. Write that in your user sandbox and test it by previewing the edit. When you are satisfied that you have no errors, save your user sandbox.

Passing information to your Lua module
Lots of uses of modules in Wikipedia require some information from the page where they are placed. We call information passed to a function a "parameter".

9. Examine Module:Sandbox/RexxS/SayHello - you should see a function called 'Hi'. Copy the function from it into your module sandbox (you already have the first and last lines, so you just need to copy the function, but make sure you copy it before the last line in your module sandbox). Save your module sandbox.

Leaving a completely blank line in your user sandbox creates a new paragraph. Typing on the next line without leaving a blank line doesn't produce a new line in the displayed text. It is useful to separate your results into new paragraphs within a section.

10. Edit your user sandbox to add this line as a new paragraph at the end:

Save it and observe the result. It calls the function 'Hi' in Module:Sandbox/RexxS/SayHello. The word 'Ada' is passed as the value of the parameter called 'name'.

This shows you how you can call code from another module, using your user sandbox. The next steps will show you how to call code from your own module sandbox, using your user sandbox.

11. Add another similar line to your user sandbox that calls the function 'Hi' with the same name (Ada), but this time from your module sandbox instead of from Sandbox/RexxS/SayHello. Save it.

12. Edit that line to change the name from 'Ada' to your name. Save it.

13. Change the first line inside the function 'Hi' of your module sandbox so that it reads:

Save it.

14. Add another line to your sandbox that is a copy of the previous line but remove the  part. Save it.

15. Make sure that your user sandbox isn't displaying any errors and is still giving you a sensible message. Can you see why the last message is "Hello from Lua to my friend Jimmy"?

We use the ' ' operator to supply a default that is used if no parameter is given.