Lua Task 2 - Working with modules edit

Prerequisite: Lua Task 1 - Introduction to Lua in Wikipedia

Creating your own Lua module edit

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 == Task 2 == on a new line at the end of your sandbox.

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

Q1. Which letter is conventionally used in Scribunto modules to hold the table of functions?
A1.

Q2. What keyword marks the start of a function definition?
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, Introduction to Lua in Wikipedia). On a new line write another similar comment with the name of this task. Save your module sandbox.

6. Below that comment, copy all of the code needed to produce the "hello" function into your your module sandbox from the example at

mw:Extension:Scribunto/Lua reference manual #Getting_started

Include the initial line beginning "local ...", and the final line beginning "return ...".

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 edit

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 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 return p). 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:

{{#invoke:Sandbox/RexxS/SayHello|Hi|name=Andy}}

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

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

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

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

strName = frame.args.name or "Jimbo"

Save it.

14. Add another line to your sandbox that is a copy of the previous line but without |name=Andy. Save it.

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