Lua first steps

3,591 views

Published on

A quick overview of Lua, an overview over the syntax, what it is good at, why it's getting more popular, and most of all: why it rocks!

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,591
On SlideShare
0
From Embeds
0
Number of Embeds
216
Actions
Shares
0
Downloads
40
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Lua first steps

  1. 1. Lua
  2. 2. Topics • History + background • Key features • Syntax • Tables • Embedding • Usecase
  3. 3. Topics • History + background • Key features Pictures • Syntax Code • Tables • Embedding • Usecase
  4. 4. 1977-1992 Trade Barriers
  5. 5. Pontifícia Universidade Católica do Rio de Janeiro
  6. 6. PUC
  7. 7. SOL (Simple Object Language) + DEL (Data Entry Language) = Lua
  8. 8. Sol
  9. 9. Lua
  10. 10. • Easy to write and understand (so no Lisp, TCL or Scheme), Python wasn’t good enough yet • Diverse collection of computers (so highly portable, not -nix only) • Should be provided as a library with a C- API
  11. 11. Influences • Scheme • Modula • CLU • C++ • SNOBOL • AWK
  12. 12. in use by
  13. 13. etc.
  14. 14. Robust
  15. 15. Fast
  16. 16. Portable
  17. 17. Embeddable
  18. 18. Powerful (yet simple)
  19. 19. Small
  20. 20. Free
  21. 21. Awesome
  22. 22. Syntax
  23. 23. if/then if true == false then print "foobar"; else print "Ponies"; end
  24. 24. while while true do print "Ponies!"; end
  25. 25. repeat/until repeat line = os.read() until line ~= "" print "Ponies";
  26. 26. Types print(type("Ponies")) --> string print(type(10.4*3)) --> number print(type(print)) --> function print(type(type)) --> function print(type(true)) --> boolean print(type(nil)) --> nil print(type(type(X))) --> string
  27. 27. Types print(type(a)) --> nil a = 10 print(type(a)) --> number a = "Ponies!!" print(type(a)) --> string a = print -- yes, this is valid! a(type(a)) --> function
  28. 28. More • ~= == != • ‘local’ for local variables (garbage collection) • bytecode execution on Lua VM (.luac)
  29. 29. Tables
  30. 30. Tables
  31. 31. Functions Functions are anonymous, and are pointers to variables a = {p = print} -- create a table a.p("Ponies!") --> Ponies! print = math.sin -- `print' now refers to the 'sin' function a.p(print(1)) --> 0.841470 sin = a.p -- `sin' now refers to the print function sin(10, 20) --> 10 20
  32. 32. Tables • Can be used to define anything you want • Classes, objects, namespaces, you name it • The only complex data structure available
  33. 33. Tables As array a = {} -- create a table and reference it in 'a' k = "x" a[k] = 10 -- new entry, with key="x" and value=10 a[20] = "Ponies!!1" -- new entry, key=20 and value="Ponies!!1" print(a["x"]) --> 10 k = 20 print(a[k]) --> "Ponies!!1" a["x"] = a["x"] + 1 -- increments entry "x" print(a["x"]) --> 11 print(a.x) --> 11
  34. 34. Tables As structures (objects) point = { x = 10, y = 20 } -- Create new table print(point["x"]) -- Prints 10 print(point.x) -- Prints 10
  35. 35. Tables As namespace Point = {} Point.new = function (x, y) return {x = x, y = y} end Point.set_x = function (point, x) point.x = x end
  36. 36. MetaTables! • The power to override events on tables, built into the interpreter • Useful for namespacing, classes, inheritance, etc.
  37. 37. MetaTables! t1 = {a = "Ponies!"} -- t1 is a table with one name-value pair. t2 = {} -- t2 is an empty table. setmetatable(t1, t2) -- t2 is t1's metatable. t3 = {c = "Rainbows!"} -- t3 is just another table like t1. t2.__index = t3 -- when a lookup fails in t1, t2 will -- look for a value in t3. print(t1.a) -- Ponies! print(t1.b) -- nil print(t1.c) -- Rainbows!
  38. 38. MetaTables! Another example t1 = {a = "Ponies!"} -- t1 is a table with one name-value pair. t2 = {} -- t2 is an empty table. setmetatable(t1, t2) -- t2 is t1's metatable. t3 = {c = "Rainbows!"} -- t3 is just another table like t1. t2.__index = function (t, k) return k -- When no value is found, return the key end print(t1.a) -- Ponies! print(t1.b) -- b print(t1.c) -- c
  39. 39. Embedding
  40. 40. Python example PyLux import pylux # Shorthand to run a Lua script with "lux('script')" def lux(script): pylux.eval("gv", "dostring", script) # Shorthand to define a Python proc as callback for Lua def luxcb(name,func): pylux.eval("gvc", "setglobal", name, func) luxGlobals=[] luxcb("luxAppend", luxGlobals.append) lux(""" for i,v in globals() do luxAppend(i) end """) luxGlobals.sort() print luxGlobals
  41. 41. Ruby example rubyluabridge et all require 'rubyluabridge' l = Lua::State.new # Create a new state for the bridge # (this is your 'sandbox') l.eval("return 1") # 1 l.n = 5 l.eval("return n") # 5 l.eval("s='Ponies!'") print l.s # Ponies!
  42. 42. Usecase
  43. 43. Background • Medianetwork • max 10gbit/s • Millions of request/day (10 million+) • Only binary data • Custom solution built in CProxy (old) • Needed new feature set • API with several subsystems
  44. 44. Background • Needed easy maintenance • Scalable, lightweight, fast • Future proof • Features! • No exposure of backend storage
  45. 45. Decisions • nginx • mod_lua • mod_cache • mod_proxy • LuaSockets
  46. 46. Bitmovr.lua • Lua • Memcached • Supports only GET • Proxy • Using filesinks => bitmovr
  47. 47. Architecture Loadbalancer lighttpd edge lighttpd lighttpd lighttpd lighttpd lighttpd nginx Loadbalancer MediaTool Castor
  48. 48. LuaSocket -- include libraries socket = require("socket"); -- create a TCP socket and bind it to the local host at the given port server = assert(socket.bind('0.0.0.0',50000)) -- loop forever waiting for clients while 1 do -- wait for a connection from any client local client = server:accept() -- make sure we don't block waiting for this client's line client:settimeout(10) -- receive the line local line, err = client:receive() -- if there was no error, send it back to the client if not err then -- send the same response as was send client:send(line); -- close the connection client:close() end end
  49. 49. LTN12 Sinks, Pumps, Filters, Chains, etc. -- load the ltn12 module local ltn12 = require("ltn12") -- copy a file ltn12.pump.all( ltn12.source.file(io.open("original.png")), ltn12.sink.file(io.open("copy.png")) )
  50. 50. LTN12 Sinks, Pumps, Filters, Chains, etc. local outputSink = socket.sink("close-when-done", client) local r, c, h = socket.http.request{ sink = outputSink, method = 'GET', url = storageLocation, redirect = true }
  51. 51. Conclusion • Fast, simple, elegant, flexible • A bit of a hurdle • Sandboxing is interesting
  52. 52. Questions?

×