Lua Introduction For New Programmers: Download Slides at
Lua Introduction For New Programmers: Download Slides at
Hello world
In [[Module:Hello]] put:
local p = {} function p.hello() return 'Hello, world!' end return p
if
if colour == 'black' then cssColour = '#000' end
if
if colour == 'black' then cssColour = '#000' else cssColour = colour end
if
if colour == 'black' then cssColour = '#000' elseif colour == 'white' then cssColour = '#fff' else cssColour = colour end
Equality
for
Types
Logic
Functions
Calling functions
colour = getDivColour()
Defining functions
local function getDivColour() return 'blue' end
Functions
Functions
Two types of functions Local functions for private use within the module
local function plural(word) return word .. 's' end
Exported functions
local p = {} function p.hello() return 'hello' end return p
Local variables
function getDivStart() colour = getDivColour() return '<div style="background-color: ' .. colour .. '">' end colour = 'Fuschia' return getDivStart() .. colour .. '</div>'
Local variables
function getDivStart() local colour = getDivColour() return '<div style="background-color: ' .. colour .. '">' end colour = 'Fuschia' return getDivStart() .. colour .. '</div>'
Local variables
Tables
Creating a table
numbers = one = two = three } { 1, 2, = 3
Numbered tables
africanFlatbreads = { 'Aish Mehahra', 'Injera', 'Lahoh', 'Ngome' } return africanFlatbreads[2] -- returns 'Injera'
Strings
Length
s = 'hello' return #s -- returns 5
sub
s = 'hello' return s:sub(2, 3) return s:sub(2) return s:sub(-2) -- returns 'el' -- returns 'ello' -- returns 'lo'
Further reading
lua-users.org
Lexical
optional, discouraged
Data types
nil Numbers
Strings
boolean
Data types
Functions
First class values Return multiple values Multiple return values are not bundled into a data type Anonymous syntax:
x = function () ... end
Data types
Tables
Implemented as a hashtable Used for OOP, like JavaScript Literal syntax: {name = value} or {a, b, c} Access with foo.bar or foo['bar']
Operators
Not equals: ~= instead of != Concatenation: .. Length: # Logical: and, or, not Exponentiation: ^ Usual meanings: <, >, <=, >=, ==, +, -, *, /, %
Operator omissions
Assignment
Like BASIC, assignment is a complete statement, not an expression Multiple assignment: a, b = c, d a, b = foo()
Control structures
Explicit block
do end ...
Precondition loop
while cond do ... end
Postcondition loop
repeat ... until cond
Control structures
If
if cond then ... elseif cond then ... else ... end
Control structures
Numeric for
for i = start, stop do ... end
Generic for
for i in iterator do ... end
Variables
Lexical scoping, almost identical to JavaScript An unset variable is identical to a nil variable
No special syntax for deletion, just x = nil No error raised for access to undefined variables
Objects
Made from tables using a variety of syntaxes, similar to JavaScript Private member variables implemented using lexical scoping, as in JavaScript Dot for static methods: obj.func() Colon for non-static methods: obj:func()
Objects
Metatables
Each table may have an attached metatable Provides operator overloading "index" metatable entry is used for object inheritance and prototype-based OOP
MediaWiki/Lua interface
Module namespace
All Lua code will be inside the Module namespace Code editor provided
Invocation
{{ #invoke: module_name | function_name | arg1 | arg2 | name1 = value1 }} #invoke instances are isolated, globals defined in one are not available in another Only caches are shared
Module structure
not isolated
Return value
The exported function returns a wikitext string Multiple return values are concatenated Non-string return values are converted to string
Frame methods
argumentPairs()
local t = {} for name, value in frame:argumentPairs() do t[name] = value end
getParent()
Provides access to the parent frame, i.e. the arguments to the template which called #invoke
Frame methods
Wikitext preprocessing
frame:preprocess('{{template}}')
Avoiding double-expansion
Arguments are already expanded Don't construct preprocessor input from arguments Use frame:expandTemplate()
Future directions
Future directions
Interwiki module invocation Languages other than Lua Date/time functions Direct access to other core parser functions and variables
Further reading
Try it out
Go to http://scribunto.wmflabs.org/ Create a function that takes several arguments and does something to them
local p = {} function p.hello(frame) return 'Hello ' .. frame.args[1] end return p