E a Lua - não a Ruby
"This is Lua - it's not Ruby"
At the November 2012 Austin.rb meeting, we introduce the Lua programming language from the point of view of a Rubyist. Lua is a language with many interesting applications, a fascinating history, and a lot of visibility in a wide array of uses from embedded scripting engines to cross-platform mobile game programming.
This repository contains heavily commented code examples for showing how to do various "getting started"-style things with Lua.
Lua was born (and is still officially maintained) at the Pontifícal Catholic University of Rio de Janeiro. The Tecgraf research group there was asked to help the state oil company, PETROBRAS, by creating a language that could be used for manipulating large data sets used in simulations.
PETROBRAS' status as a state-owned concern included many restrictions on the way the company could spend money, especially as it applied to importing computer technology. The language tools that PUC/Tecgraf was developing would need to run on any of the various kinds of hardware that the massive oil company had in use -- everything from Unix to Windows to MacOS to a host of embedded systems.
Among Tecgraf's first efforts was a specialized configuration langugage called SOL (Structured Object Language) which allowed for type definitions and other features of data description. By the time the Tecgraf team had completed an implementation, they realized they'd need something more robust - "a real programming language, with assignment, control structures, sub-routines, and the like" - that could be used for general purpose programming.
Because the language evolved from SOL ("Sol" means "Sun" in Portuguese), someone suggested that the new language could be called "Lua", which means "moon".
Lua has been in active use since its introduction in 1994.
Lua makes a lot of sense, but there are a number of gotchas you might want to check out. Among them are these:
- A table is sort of like an array, sort of like a hash, and is Lua's only complex data type.
- Indices for tables start at 1 instead of 0
- All numbers are floating point
- Functions can return multiple values
- Regular stuff like socket IO is implemented in modules instead of Lua's stdlib in order to keep the core of the language as small as possible. Keep in mind that Ruby's stdlib is pretty big (xml parsers, web servers, templating, lots of protocols, etc), so we're kind of spoiled.
- Patterns are NOT full-on (i.e. POSIX/PCRE) regex - even if they sort of look like they are
- Table keys don't look like strings. They look like undeclared variables, but… they work :-)
Lua is popping up all over the place. Its portability has always been a core feature, and that ability to be embedded in such a small footprint has made it the language of choice for multiple software projects' native scripting engines, as well as a higher-level language used to produce code in things like game frameworks. Here are a few of the places it's in use today:
- Redis scripting
- Nmap scripting
- CoronaSDK (iOS/Android 2-D game development)
- World of Warcraft interface scripting
- Various pieces of hardware (including Lego Mindstorms NXT)
- A bunch of Lua-scriptable game engines
- Tons of games you can straight-up script with Lua (132+)
We will need two things: the Lua interpreter and the LuaRocks package system (similar to RubyGems)
Installing on OSX
Note: you need to update Homebrew to ensure that you catch a recent patch to make Lua 5.1.4 the default instead of the more bleeding edge 5.2. Lots of libraries aren't fully compatible w/ 5.2 yet, including ones we're using tonight.
$> brew update $> brew install lua luarocks $> luarocks install luasocket
Installing on (Ubuntu 12.04) Linux
$> sudo apt-get install lua5.1 luarocks $> sudo luarocks install luasocket
After you've installed Lua, you can get into the interactive shell like this:
$> lua Lua 5.1.4 Copyright (C) 1994-2008 Lua.org, PUC-Rio > print('hello, world!') hello, world! >
Exit the shell with Ctrl-D