Introduction to Lua

This is the first entry in my Lua Tutorial series. Check out this for our next entry covering “Hello World”.

Lua (https://www.lua.org/) is a cool interpreted language which is typically known for being embedded in programs as a scripting language to extend programs, but it is also a fast, light, portable standalone language. The core language is extremely simple, but that doesn’t mean it can only do simple tasks. I have personally used Lua to replace Perl because it is much easier to deploy on Windows, and also because it is much lighter and more agile for some of the types of projects I do on Linux as well.

Lua supports most things modern languages do, and is extremely extensible. Library support is also pretty good with things like luarocks (https://luarocks.org/) which should be available from any Linux distribution’s package manager, and can easily be found online for download from a couple projects or from somewhere like http://luabinaries.sourceforge.net/. I won’t cover installation in this series since there are several different ways, and none are particularly difficult for most people.

For all of the full script examples I include in this series, I will be using the syntax you would use on a standard Linux or Mac OS X environment. This will basically amount to a one-line difference and shouldn’t impact the way anything is done in any script or run in any script as I am going to try and keep everything very standard and very clean where possible (I use the same convention on Windows which completely ignores it). I will cover the various features in Lua relatively briefly and drop some resources near the end for further reading. The goal of this tutorial series is to make someone new to programming and new to Lua able to use Lua in some functional capacity, and to be able to make sense of higher level materials to really take control of the language and do what you want with it.

This series will also mainly target 5.1, but everything should be compatible with newer versions. I try to write for Lua how I write for Perl, that is, using an early version (5.8 in the case of Perl) as the basis for everything, and adding newer features only when necessary to prevent myself from needing a workaround. I chose 5.1 due to some of the libraries, support, and because it was the most universally stable in the environments I need to use it on. Lua stays backwards compatible for the most part, so what works in 5.1 will work in 5.3, so only newer features really need to be added.

This tutorial will progress from the basics of even making and running a program, to covering some of the basic features for user interaction, then moving on to variables, logic, I/O, and then will progress on to using libraries and extending what you can do with OOP and similar. I will try to make these articles written so that anyone without experience can learn from them, but I will also try to structure them so that people familiar with other languages have a jumping off point without too much drudgery. After we get through the language basics, I will post some resources to help keep going.

The Lua organization publishes a book called “Programming in Lua” which I would recommend as a reference resource. I have not reviewed the newest edition, but the older editions were a little hard to use a sole resource for learning the language unless you are a fan of highly modular, technical reading. It is a great resource once you are familiar with the language and the explanations and examples will really take your usage to the next level for making the language really do what you want it to do.

Our next installment will cover the basics of using Lua and running a program. This will cover the usual “hello world” and all of the other niceties of getting started. Get familiar with the installation process and we’ll get moving with the rest on the next installment. Let’s get to writing Lua!