Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| Beide Seiten der vorigen Revision Vorhergehende Überarbeitung Nächste Überarbeitung | Vorhergehende Überarbeitung | ||
|
becki:linux:lua [2014-01-29 09:28] becki |
becki:linux:lua [2025-11-12 12:09] (aktuell) becki [Documentation Generation] |
||
|---|---|---|---|
| Zeile 12: | Zeile 12: | ||
| * [[http://lua-users.org/wiki/LibrariesAndBindings|Manual]] | * [[http://lua-users.org/wiki/LibrariesAndBindings|Manual]] | ||
| - | ===== Standalone lua == | + | ===== Implementations == |
| - | ==== Shebang == | + | ==== Standalone lua == |
| + | === Shebang == | ||
| ''#!/usr/bin/lua'' | ''#!/usr/bin/lua'' | ||
| - | ==== Command Line Arguments == | + | === Command Line Arguments == |
| Use global table ''arg'' | Use global table ''arg'' | ||
| - | ==== StdIo == | + | === StdIo == |
| <code lua> | <code lua> | ||
| Zeile 29: | Zeile 30: | ||
| io.stderr:write() -- Print a string to stderr | io.stderr:write() -- Print a string to stderr | ||
| </code> | </code> | ||
| + | |||
| + | ==== Lua for Web Browsers == | ||
| + | |||
| + | [[http://kripken.github.io/lua.vm.js/script_example.html|lua.vm.js]] thanks to asm.js | ||
| + | |||
| + | ==== Lua for Java/Android == | ||
| + | |||
| + | [[https://sourceforge.net/projects/luaj/|Luaj]], seen at [[https://github.com/M66B/XPrivacyLua/blob/master/README.md|XPrivacyLua]] | ||
| ===== C-style ?-operator equivalent == | ===== C-style ?-operator equivalent == | ||
| Zeile 44: | Zeile 53: | ||
| <code lua> | <code lua> | ||
| d1, d2 = table.unpack(a and {b1, b2} or {c1, c2}) | d1, d2 = table.unpack(a and {b1, b2} or {c1, c2}) | ||
| + | </code> | ||
| + | |||
| + | <note warning>Take care if b can become ''nil'' or ''false''. In this case the equivalent doesn't work. Instead use:</note> | ||
| + | <code lua> | ||
| + | if a then d=b else d=c end | ||
| + | </code> | ||
| + | |||
| + | ===== Default values for missing arguments == | ||
| + | |||
| + | <code lua> | ||
| + | function foo(a) | ||
| + | a= a or "defaultvalue" | ||
| + | end | ||
| + | </code> | ||
| + | |||
| + | Take care, if ''false'' is a valid value, instead use: | ||
| + | |||
| + | <code lua> | ||
| + | function foo(a) | ||
| + | if a==nil then a="defaultvalue" end | ||
| + | end | ||
| + | </code> | ||
| + | |||
| + | |||
| + | ===== Difference between nil and no value == | ||
| + | <code lua> | ||
| + | function return_nil() | ||
| + | return nil | ||
| + | end | ||
| + | |||
| + | function return_nothing() | ||
| + | end | ||
| + | |||
| + | function count_args(...) | ||
| + | print(table.pack(...).n) | ||
| + | end | ||
| + | |||
| + | count_args(nil) --> 1 | ||
| + | count_args() --> 0 | ||
| + | --=> Passed nils increment argument count | ||
| + | |||
| + | count_args(return_nil()) --> 1 | ||
| + | count_args(return_nothing()) --> 0 | ||
| + | --=> Returned nils increment return value count | ||
| + | |||
| + | print(tonumber(nil)) --> nil | ||
| + | print(tonumber(return_nil())) --> nil | ||
| + | --=> tonumber() accepts nil as argument | ||
| + | |||
| + | print(tonumber()) --> bad argument #1 to 'tonumber' (value expected) | ||
| + | print(tonumber(return_nothing())) --> bad argument #1 to 'tonumber' (value expected) | ||
| + | --=> tonumber() does not accept nothing as argument | ||
| + | </code> | ||
| + | |||
| + | <note warning>An example for a function returning nothing is the result of string.gmatch()</note> | ||
| + | |||
| + | ===== Lexical scoping == | ||
| + | |||
| + | <code lua> | ||
| + | local strict= require"fs.strict" | ||
| + | local g,h,i | ||
| + | |||
| + | local function f() | ||
| + | g() -- ok | ||
| + | h() -- ok | ||
| + | --i() -- err, because i refers to old upvalue which is still nill | ||
| + | end | ||
| + | |||
| + | g= function() end -- existing upvalue g is assigned a function | ||
| + | function h() end -- same as above (mind the missing "local"!) | ||
| + | local function i() end -- a new upvalue is created which hides old i(assumption) | ||
| + | |||
| + | f() | ||
| + | i() -- ok | ||
| </code> | </code> | ||
| Zeile 131: | Zeile 214: | ||
| ===== Error Handling == | ===== Error Handling == | ||
| + | ==== pcall() differs from Lua Convention == | ||
| + | ''pcall()'': | ||
| + | success: true, value(s) | ||
| + | failure: false, error | ||
| + | |||
| + | Lua [[http://lua-users.org/wiki/FinalizedExceptions|convention]]: | ||
| + | success: value(s) | ||
| + | failure: nil, error | ||
| + | |||
| + | <note tip>Error handling code after ''pcall()'' differs from error code after calling a function which complies to the convention!</note> | ||
| + | |||
| ==== Raise error or return error? == | ==== Raise error or return error? == | ||
| === Converting between raise and return == | === Converting between raise and return == | ||
| Zeile 163: | Zeile 257: | ||
| ===== Classes and Objects == | ===== Classes and Objects == | ||
| + | |||
| + | See also projects/accounting/sbaccimport which has since rev 482:c85c255f1870 classes with inheritance where the constructor new() is a class method and can therefore be reused in child classes similar to PIL capter 21 | ||
| ==== Classes as Object templates == | ==== Classes as Object templates == | ||
| <code lua> | <code lua> | ||
| Zeile 382: | Zeile 478: | ||
| </code> | </code> | ||
| + | ===== File access == | ||
| + | |||
| + | Each of the following constructs is equivalent, it reads a file line by line: | ||
| + | |||
| + | <code lua> | ||
| + | local f= io.open"file.txt" | ||
| + | repeat | ||
| + | local l= f:read("l") | ||
| + | if l then print(l) end | ||
| + | until not l | ||
| + | f:close() | ||
| + | </code> | ||
| + | |||
| + | <code lua> | ||
| + | local f= io.open"file.txt" | ||
| + | for l in f:lines("l") do | ||
| + | print(l) | ||
| + | end | ||
| + | f:close() | ||
| + | </code> | ||
| + | |||
| + | <code lua> | ||
| + | for l in io.lines("file.txt", "l") do | ||
| + | print(l) | ||
| + | end | ||
| + | </code> | ||
| + | |||
| + | - ''"l"'' as arg for ''read()'' is the default, it can be omitted | ||
| + | - ''file:lines()'' and ''io.lines()'' accept the same format args as ''read()''. (Tested. Nowhere found in docu). Thus you could read a file eg chunk by chunk as well. | ||
| + | - io.lines() without arguments returns an iterater wich returns stdin line by line | ||
| + | |||
| + | ==== Load CSV-like files == | ||
| + | |||
| + | <code lua> | ||
| + | #!/usr/bin/lua | ||
| + | local separator=":" | ||
| + | for line in io.lines() do | ||
| + | print(line) | ||
| + | for cell in line:gmatch("[^"..separator.."]+") do | ||
| + | print("", cell) | ||
| + | end | ||
| + | end | ||
| + | </code> | ||
| ===== Includes and Modules == | ===== Includes and Modules == | ||
| ==== Include == | ==== Include == | ||
| Zeile 639: | Zeile 778: | ||
| [[http://stevedonovan.github.io/ldoc/topics/doc.md.html|Ldoc]] is preferred (over [[http://keplerproject.github.io/luadoc/manual.html#howto|LuaDoc]]). | [[http://stevedonovan.github.io/ldoc/topics/doc.md.html|Ldoc]] is preferred (over [[http://keplerproject.github.io/luadoc/manual.html#howto|LuaDoc]]). | ||
| + | |||
| + | {{tag>lua}} | ||
| + | |||