Getting Started With Lua
I recently had a chance to refamiliarize myself with lua for a project
at work. A few years ago I spent some time dabbling with Corona SDK,
and by extension, with lua. My first impression was that Lua was
adequate for scripting tasks, although the syntax for OOP was clunky,
and I couldn't see a lot of use for it beyond the Corona SDK platform.
In any case, I thought I'd give it another try.
I was impressed with how easy it was to get started with lua. I'm on a mac, and the main page has a download link to source code but no pre-compiled binaries. Ok, no problem, I'm a *nix nerd and I'm not afraid of building from source. I was impressed at just how easy that actually was. The entire codebase is just a few dozen files and compiled in ~ 3 seconds on my mac mini.
I love languages that I can fit into my pocket, and lua just caught my attention!
Here's what to do to build Lua from source:
If you're on linux, change "macosx" to "linux". If you're on windows, you'd better go read the instructions at www.lua.org.
Once I'd built the binaries, I had three files that I cared about in the src/ directory:
Again, nice and lean. lua is the actual interpreter, which is how you will run Lua code. luac is the Lua compiler, which translates lua source code into bytecode. liblua.a is the static lua library, which makes it easy to embed Lua as a scripting language into other applications.
We're going to play around with the Lua stand-alone interpreter tonight. This is the application that you'll use if you want to run a lua script, but if you call it without an argument, you'll get the interactive mode (REPL).
Traditional first program:
Global variable:
In Lua, all variables are added to the global symbol table by default. This isn't what we want (probably) so let's make the variable local. Easy:
Well, you might be in for a surprise at this point if you're following along using the interactive interpreter. Instead of seeing "I'm local, and hello!" as output, you'll see "Hello World", which was what we last assigned to the variable hello. The reason for this is that the interactive mode runs each line as its own scope. So this will work:
But the original example doesn't do what you think it should because the line that does the printing, the variable hello is already out of scope. Lua then falls back to the hello in the global scope, and you get "Hello World". Just something to be aware of as you try things out in the REPL.
Because of this, if you want to handle more than one line of code you'll need to use a code block. You can do it like this:
This will give you the expected output.
In just a handful of minutes we've been able to pull down source code, build it and start trying out Lua in the interpreter. I think it's impressive to see how lithe and lean this language is, and it makes perfect sense why it is so popular as an embedded scripting language.
In any case, I thought I'd give it another try.
I was impressed with how easy it was to get started with lua. I'm on a mac, and the main page has a download link to source code but no pre-compiled binaries. Ok, no problem, I'm a *nix nerd and I'm not afraid of building from source. I was impressed at just how easy that actually was. The entire codebase is just a few dozen files and compiled in ~ 3 seconds on my mac mini.
I love languages that I can fit into my pocket, and lua just caught my attention!
Here's what to do to build Lua from source:
curl -R -O http://www.lua.org/ftp/lua-5.2.3.tar.gz
tar zxf lua-5.2.3.tar.gz
cd lua-5.2.3
make macosx test
If you're on linux, change "macosx" to "linux". If you're on windows, you'd better go read the instructions at www.lua.org.
Once I'd built the binaries, I had three files that I cared about in the src/ directory:
lua
luac
liblua.a
Again, nice and lean. lua is the actual interpreter, which is how you will run Lua code. luac is the Lua compiler, which translates lua source code into bytecode. liblua.a is the static lua library, which makes it easy to embed Lua as a scripting language into other applications.
We're going to play around with the Lua stand-alone interpreter tonight. This is the application that you'll use if you want to run a lua script, but if you call it without an argument, you'll get the interactive mode (REPL).
Traditional first program:
print("Hello World from Lua!")
Global variable:
hello = "Hello World"
print( hello )
In Lua, all variables are added to the global symbol table by default. This isn't what we want (probably) so let's make the variable local. Easy:
local hello = "I'm local, and hello!"
print( hello )
Well, you might be in for a surprise at this point if you're following along using the interactive interpreter. Instead of seeing "I'm local, and hello!" as output, you'll see "Hello World", which was what we last assigned to the variable hello. The reason for this is that the interactive mode runs each line as its own scope. So this will work:
local hello = "I'm local, and hello!"; print(hello)
--hello is in scope at the end of the line above
But the original example doesn't do what you think it should because the line that does the printing, the variable hello is already out of scope. Lua then falls back to the hello in the global scope, and you get "Hello World". Just something to be aware of as you try things out in the REPL.
Because of this, if you want to handle more than one line of code you'll need to use a code block. You can do it like this:
do
local hello = "This time I'm local"
print( hello )
end
This will give you the expected output.
In just a handful of minutes we've been able to pull down source code, build it and start trying out Lua in the interpreter. I think it's impressive to see how lithe and lean this language is, and it makes perfect sense why it is so popular as an embedded scripting language.
Comments
Post a Comment