Main Content

Lua examples - coroutines, error handling, objects, etc

Archive - Originally posted on "The Horse's Mouth" - 2009-10-15 08:55:03 - Graham Ellis

I have presented a bespoke Lua course over the last three days ... and it has lead me to some interesting new examples which I'm sharing here.

Object Orientation is more a way of thinking and coding than a language feature in Lua - when you set up a piece of data / structure / object, you do so in a table, and you include in the table references to the functions that are to be called when you perform certain actions on that table. See here for a first example (which never the less shares some functionality between different types of data - polymorphism) and here for an example which also overrides various default methods through a metatable. The data these two sample programs use may be found here.

Coroutines provide the Lua programmer with the ability to have several functions active at the same time - akin to parallel processing or threading, if you like to think of it that way. A comparison is sometimes made to Python's generators, but with Lua coroutines you can easily have several active at the same time. This is a great way of storing "state" within a function - it saves messy parameter passing and globals, and it also saves really long blocks of code too. If you want an iterator - anything that you might say "give me the next ..." to, then it's probably best to use a coroutine. There's a new example showing coroutine basics here, and an example in which we have used several co-routines to process different steps in a process here.

If you open a file in Lua, you'll get a file handle back ... or nil if the file fails to open. But WHY did it fail to open? io.open actually returns a second value if the first one is nil - an error string. There's an example here.

During our courses, we set a number of practical exercises so that delegates can try things out, calling on the tutor to talk about the best way to design and implement a task if they wish, with regards to code readability and maintainability. Such as exercise is set here ... calling for the delegates to write a module to meet what is, in essence, a specification and test harness. My own sample answer, written after the delegates had come up with their own answers, is here

In Lua, functions are truely dynamic - you can load them then overwrite them, and you can even define them within conditionals so that they'll be defined in different ways depending on your data. Powerful but dangerous! There's a sample showing you the mechanism here ... and we discuss the pros and cons of using these techniques on our Lua Programming and Learning to program in Lua courses (the former for people with prior programming experience, the latter for those who have never programmed before).

A log file analysis program that I wrote about 2/3 of the way through the course pulled together all the common coding features you might use in a shortish piece of code - loading data from a file, extracting information from it, sorting and producing a formatted report.

And a final example shows how you can split a line of text using string.find and string.sub ... there are other ways (such as string.gfind) which make for shorter code - there's an example of string.gfind here where it is used for filter email addresses from a flow of data.

Please follow the links on this page to see the various source codes ... please have a look at our Lua courses if you want to learn more from us. Public courses run in Melksham, Wiltshire, England; if you have a group of people, we'll run a private course for you - either at our centre, or at your office almost anywhere in the world.