Introduction - Beginning Lua Programming (2007)

Beginning Lua Programming (2007)


Perhaps you need one or more of these things:

· A way to present dynamic information, both textual and graphical, on your website

· A means to transfer legacy data to a modern database

· Nonprogrammers or end users to augment your application with additional functionality

· A custom program for your handheld device that you can use in the field

· Scripts to drive the user interface and business logic of an enterprise-level application

· An engine to run gaming scripts

· An interface language for scientific instrumentation

· A scripted way to monitor the health of a computer network

· A robust mechanism to allow end users to set application options in an easy-to-understand configuration file

If so, you'll find this versatile and fast programming language called Lua to be the perfect tool. Lua has a gentle learning curve that will enable you to write effective programs after only a short introduction. With it, simple programs look simple—there is no extraneous baggage you need to add to your programs or peculiar syntax to which you need to conform to make them run. From the examples in the preceding list, you can see that Lua is quite appropriate for use by technically adept individuals who aren't necessarily programmers.

At the other end of the continuum, Lua has features that support advanced program requirements. It imposes very few conventions on the way you write your programs, instead providing mechanisms with which you can construct clear and maintainable solutions to your programming tasks. Even experienced software developers find novel and powerful ways of using Lua to extend and simplify their applications.

Lua is robust, yet its mild entry curve makes it quite suitable as a first programming language. In combination, these make Lua an attractive language for students and professionals alike.

The Facets of Lua

Lua is, first and foremost, a tool for creating software. You can use the standalone interpreter that is packaged with the Lua distribution to great advantage, and fit it seamlessly into your applications.

Lua Is a Programming Language

Lua as a language has its own grammar and idioms. Like all languages, it is a means to communicate, and like all programming languages, it can be used to convey instructions to a computer. But this connection to hardware isn't essential. In fact, Edsger Dijkstra, one of the towering figures of computer science, emphasized the importance of programming without a computer to really understand and verify programs. Lua's syntax—the rules that dictate how its language pieces may fit together correctly—is small, clean, and straightforward. This syntax includes ways to convey instructions as well as to describe data.

Lua Is an Implementation

Lua is also a functioning software system. A part of what we call Lua is an actual computer application that can interpret programs written in the Lua programming language. The Lua interpreter is written in ANSI C, which because of its wide support, allows Lua to run on a vast spectrum of devices from high-end network servers to small devices.

Both Lua's language and its interpreter are mature, small, and fast. Both have been synthesized from some of the best ideas and practices in computer science. The smallness of Lua is by design, and has advantages well beyond Lua's capability to run on tiny hardware. A few visits to Lua's mailing list will assure you that there are enthusiasts who understand every nook and cranny of this language and its implementation. Its source code has been scrutinized. It can be argued that these insights, and the suggestions for refinements that these insights foster, would be much less likely with a larger language and implementation.

Lua Is Fast

Traditionally, programming language ease-of-use has come at the cost of performance. The C programming language is known for its speed and extensive library support, but it is rarely categorized as easy to use. Lua alters the playing field somewhat by being both easy to use and fast, and it has the ability to interface smoothly with C libraries. How fast is Lua? In a word: very. A visit to the programming language shootout site ( should convince you that with Lua, speed and expressivity are not mutually exclusive.

Lua Is Free and Open

Lua is open-source software. You can use it in personal, academic, and commercial applications at no cost. Your essential requirements when using Lua are to properly ascribe its copyright (, PUC-Rio) and to not hold its authors or copyright holders liable if anything goes wrong. You can read its license at Be aware that some of the libraries you will use with Lua are licensed under different terms. Please understand and adhere to these licenses. A lot of hard work and ingenuity goes into the creation of software, and your respect for its authors' intentions helps keep the open software community vibrant and active.

Who This Book Is For

This book is for students and professionals who are intrigued by the prospect of learning and using a powerful language that provides a rich infrastructure for creating programs. No programming knowledge is necessary to benefit from this book except for the section on Lua bindings, which requires some familiarity with the C programming language. A certain comfort level with command-line operations, text editing, and directory structures is assumed.

Software developers who have experience with functions, strings, and associative arrays can skim Chapters 2 through 5 with the caveat that certain Lua colloquialisms are introduced there along with programming concepts.

Throughout the text, sections pertaining to a particular operating system are clearly marked and can be skipped by readers working on a different platform.

How This Book Is Structured

This book is organized to guide you through the basics of using Lua. Its structure is as follows:

· Installing Lua on your system (Chapter 1)

· Learning the fundamentals of programming in Lua (Chapters 2 through 10)

· Reviewing standard Lua functions (Chapter 11)

· Exploring application development with Lua using packages contributed by the community (Chapters 12 through 18)

· Using Lua's many community resources (Chapter 19)

Chapters 2 through 10 each build on concepts that are presented in its predecessors, so a sequential reading of this part of the book is advised. The summary of Lua's built-in libraries contains examples that assume you have a good grasp of the materials presented in the first 10 chapters.

Some of the libraries and techniques presented in Chapters 12 and 13 are needed in the remaining chapters of the book. Chapters 14 through 19 are relatively independent of one another and can be read out of sequence.

What You Need to Use This Book

You need surprisingly little in the way of computer resources to learn and use Lua. This book focuses on Windows and Unix-like (including Linux) systems, but any operating system that supports a command shell should be suitable. You'll need a text editor to prepare and save Lua scripts.

If you choose to extend Lua with libraries written in a programming language like C, you'll need a suitable software development kit. Many of these kits are freely available on the Internet but, unlike Lua, they can consume prodigious amounts of disk space and memory.

Chapter 18 discusses using Lua on a Palm Pilot. Even if you don't own or have access to one of these devices, this chapter shows how you can simulate one on the major desktop systems.


To help you get the most from the text and keep track of what's happening, a number of conventions are used throughout the book.

Try It Out

This is an exercise you should work through, following the text in the book.

1. A Try It Out usually consists of a set of steps.

2. Each step has a number.

3. Complete all the steps, sequentially, to produce the intended results.

How It Works

After each Try It Out, the code you've typed is explained in detail.

Boxes like this one hold important, not-to-be forgotten information that is directly relevant to the surrounding text.

Tips, hints, tricks, and asides to the current discussion are offset and placed in italics like this.

As for styles in the text:

· New terms and important words are highlighted when they're introduced.

· Keyboard strokes look like this: Ctrl+A.

· Filenames, URLs, and code within the text look like so:

· Code is presented in two different ways:

In code examples, new and important code is highlighted with a gray background.

The gray highlighting is not used for code that's less important in the present

context, or has been shown before.

Some of the code examples are mixtures of your input and Lua's output:

> In such examples, your input is bold

and Lua's output is not.

For author and peer discussion, join the P2P forums at The forums are a Web-based system for you to post messages relating to Wrox books and related technologies and interact with other readers and technology users. The forums offer a subscription feature to e-mail you topics of interest of your choosing when new posts are made to the forums. Wrox authors, editors, other industry experts, and your fellow readers are present on these forums.

At you will find a number of different forums that will help you not only as you read this book, but also as you develop your own applications. To join the forums, just follow these steps:

1. Go to and click the Register link.

2. Read the terms of use and click Agree.

3. Complete the required information to join as well as any optional information you wish to provide and click Submit.

4. You will receive an e-mail with information describing how to verify your account and complete the joining process.

You can read messages in the forums without joining P2P but to post your own messages, you must join.

After you join, you can post new messages and respond to messages other users post. You can read messages at any time on the Web. If you would like to have new messages from a particular forum e-mailed to you, click the Subscribe To This Forum icon by the forum name in the forum listing.

For more information about how to use the Wrox P2P, be sure to read the P2P FAQs for answers to questions about how the forum software works as well as many common questions specific to P2P and Wrox books. To read the FAQs, click the FAQ link on any P2P page.