xv
Nmap users tend to think of Lua as the language of the Nmap Scripting Engine;
players of World of Warcraft may regard Lua as a language exclusive to that
game.
Lua is useful also as a stand-alone language, not only for text-processing and
one-shot little programs, but increasingly for medium-to-large projects, too. For
such uses, the main functionality of Lua comes from libraries. The standard
libraries, for instance, offer basic pattern matching and other functions for
string handling. As Lua improves its support for libraries, there has been a
proliferation of external packages. Lua Rocks, a deployment and management
system for Lua modules, currently features more than 150 packages.
Finally, there are those programmers that work on the other side of the
bench, writing applications that use Lua as a C library. Those people will
program more in C than in Lua, although they need a good understanding of
Lua to create interfaces that are simple, easy to use, and well integrated with
the language.
This book has much to offer to all these people. The first part covers the
language itself, showing how we can explore all its potential. We focus on
different language constructs and use numerous examples and exercises to show
how to use them for practical tasks. Some chapters in this part cover basic
concepts, such as control structures, while others cover topics more advanced,
such as iterators and coroutines.
The second part is entirely devoted to tables, the sole data structure in Lua.
Its chapters discuss data structures, persistence, packages, and object-oriented
programming. There we will unveil the real power of the language.
The third part presents the standard libraries. This part is particularly
useful for those that use Lua as a stand-alone language, although many other
applications also incorporate all or part of the standard libraries. This part
devotes one chapter to each standard library: the mathematical library, the
bitwise library, the table library, the string library, the I/O library, the operating
system library, and the debug library.
Finally, the last part of the book covers the API between Lua and C, for those
that use C to get the full power of Lua. The flavor of this part is necessarily
quite different from the rest of the book. There, we will be programming in C,
not in Lua; therefore, we will be wearing a different hat. For some readers, the
discussion of the C API may be of marginal interest; for others, it may be the
most relevant part of this book.
About the Third Edition
This book is an updated and expanded version of the second edition of
Program-
ming in Lua
(also known as the
PiL 2 book
). Although the book structure is
virtually the same, this new edition has substantial new material.
First, I have updated the whole book to Lua 5.2. Of particular relevance is the
chapter about environments, which was mostly rewritten. I also rewrote several
examples to show how to benefit from the new features offered by Lua 5.2.