8. Appendix¶
8.1. Workshop media¶
A media is provided which contains all you need to do the Haka workshop. You can download it from our web-site in the section Resources.
This media contains a full bootable live system that contains all dependencies of Haka (usable in a virtual machine (VirtualBox for instance) or directly as a bootable USB or CD.
8.2. Lua¶
Lua is a simple, lightweight and powerful language. It is used inside Haka to define configurations.
The full documentation is available at http://www.lua.org/.
Some basic tutorial to introduce Lua are also available at http://lua-users.org/wiki/TutorialDirectory.
You can do some tests with the lua interpreter :
$ lua Lua 5.2.1 Copyright (C) 1994-2012 Lua.org, PUC-Rio >
The following is aimed at giving a quick overview of Lua language.
8.2.2. Variables¶
Lua variables are global except if they are preceded with local keyword. It is possible to declare it anywhere.
local mylocalvar = "foo"
myglobalvar = "bar"
Note
It is strongly discouraged to use global variables.
8.2.3. Types in Lua¶
8.2.3.1. Basic types¶
Lua define the following basic types:
- nil - represent a nil value. It is also a keyword.
- boolean - represent a boolean value. Can be true or false.
- number - represent a number. Can be either an integer or a floating point number.
- string - represent a character string.
- table - represent a table of object.
- function - represent a function.
- userdata - a complex type used to map C data.
8.2.3.2. String¶
Lua allow to easily concatenate string with the .. keyword.
foo = "foo"
bar = "bar"
print(foo.." : "..bar)
To format a string, the Lua library string provides an utility function to do this.
print(string.format("%s: %d", "foo", 42)
8.2.3.3. Tables¶
As tables are used almost everywhere in Lua (and obviously in haka) you should know a few things about it. It can be used to represent an array or an object.
Lua table declaration is made with {}.
When used as an array, Lua table index starts at 1.
mytable = { "foo" }
print(mytable[1])
Getting a inexistent element of a table return nil. It is then not an error to access an element not present.
mytable = { "foo" }
print(mytable[2])
Setting an element in a table to nil simply remove it from the table.
mytable = { "foo", "bar" }
mytable[1] = nil
print(mytable[1])
print(mytable[2])
Note
Lua does not reorder the table when an element is removed from it. To do this, check the Lua library table.
Lua tables can be used as a map. Namely you can index element of a table with any type of value.
Declaration of such element are done with a simple = in declaration or by adding it a posteriori:
mytable = { foo = "myfoostring" }
mytable.bar = "mybarstring"
Access to this element can be done with the usual [] or directly with a ..
print(mytable["foo"])
print(mytable.bar)
Note
Don’t forget that accessing an inexistent element of a table return nil. Even for a table used as a map.
It is possible to mix indexed table and mapped table.
mytable = { "foo" }
mytable.bar = "bar"
print(mytable[1])
print(mytable.bar)
Lua provide two iterator function to loop on table.
First one is called ipairs() and it will loop over indexed value of the table.
Second one is called pairs() and it will loop over every value of the table.
See also
for loops for more information on how to use pairs() and ipairs().
8.2.4. Boolean logic¶
Lua offers the usual boolean operators:
- and
- or
- not
Warning
In Lua, everything is true except nil and false. For instance 0 is true.
8.2.5. Control flow¶
8.2.5.1. if-then-(else)¶
Lua if-then-(else) statement looks like:
if condition then
-- [...]
else
-- [...]
end
Note
It is not required to wrap conditions inside parenthesis.
8.2.5.2. for loops¶
Lua for loop statement looks like:
for i = first,last,delta do
-- [...]
end
Note
delta may be negative, allowing the for loop to count down or up.
Lua have a for-in loop statement:
for _, element in pairs(mytable) do
-- [...]
end
A for-in statement use the special in keyword followed by a call to one of pairs() or ipairs() functions. Both return two values: index, value.
Note
Lua recommends the use of _ for unused variable
8.2.5.3. while and repeat-until loops¶
Lua have a while loop statement:
while condition do
-- [...]
end
Lua provide an opposite loop statement called repeat-until:
repeat
-- [...]
until condition
8.2.6. Functions¶
Lua functions are declared with the function keyword.
function myfunc()
-- [...]
end
Lua function can return multiple values.
function myfunc()
return true, 1, "foo"
end
ok, count, bar = myfunc()
There is not error when calling a function with an incorrect number of arguments. The extra arguments are not used and the missing one are set to nil.
function myfunc(a, b, c)
print(a, b, c)
end
myfunc(10) -- b and c will be equal to nil
Function are first-class value in Lua. This means that they can be used exactly like a number for instance. As an example, it is possible to return a function from a function.
function myfunc(a)
return function ()
print(a)
end
end
myfunc(10)()