Slashdot Mirror


Programming in Lua 2nd Edition

Andre Carregal writes "In the second edition of Programming in Lua Roberto Ierusalimschy presents the Lua programming Language in a simple yet precise format for both novice and advanced programmers." Read the rest of Andre's review. Programming in Lua, 2nd Edition author Roberto Ierusalimschy pages 328 publisher Lua.Org; 2nd edition (March 5, 2006) rating 10 reviewer Andre Carregal ISBN 8590379825 summary Shows how Lua is the language of choice for anyone who needs a scripting language that is simple, efficient, extensible, portable, and free.

The Lua programming language has been around for more than 10 years, but only recently has it started to appear on the mainstream radar screens. It has evolved from a simple scripting language to a full-fledged solution for scripting and embedding. Lua is portable, fast and small, and the language of choice for applications as diverse as games, Web sites and robot controllers as the Lua Users Wiki and LuaForge will confirm.

Lua portability can be shown by the long list of operating systems it runs on, which includes not only Windows and Linux, but also small systems and mobile devices. The memory and CPU requirements for the runtime are really low, and Lua has been used in environments that most other scripting options would not dare go into.

One of the milestones of the history of Lua has been the release of the book Programming in Lua (a.k.a "PiL"), by Roberto Ierusalimschy, in 2003. This review is about the second edition of Programming in Lua, a.k.a. the "Blue PiL" due to the color of its cover.

Lua is free software and can be obtained from its site (www.lua.org). There you will also find the reference manual of the language and the full contents of the first edition of Programming in Lua.

As a disclaimer, let me say that I'm one of the technical reviewers of the book and I have been working with Lua since 1993, most of the time interacting with Roberto Ierusalimschy, even when not working in the same projects as him.

The second edition manages to surpass an already good book in various aspects. The "Blue PiL" presents not only the language itself, but also gives an excellent view of how one could see Lua as the foundation of solid scripting techniques. The original Programming in Lua was focused on Lua 5.0, while the second edition focuses on Lua 5.1. While the differences between Lua 5.0 and 5.1 are not radical, the newer version has a lot more to offer, both in terms of the modularity aspect and of language features. The second edition of Programming in Lua indicates clearly when some concept or detail relates only to a specific version of Lua, but most of the contents can be applied to both Lua versions.

Programming in Lua is oriented to both the novice programmer who wants to get a first feeling for Lua, and to the advanced Lua or C programmer who wants to use the full potential of the language. The book clearly separates the two aspects and indicates the sections that can be skipped for each reader profile. The author manages to keep a light and easily readable style during the whole book and the examples are simple and direct, making the flow of the reading quite pleasant. I can say that, even having some reasonable experience with Lua, I was surprised by a lot of details in the book. The "Blue PiL" is one of those books that you can read many times over, and each time you learn something new.

The "Blue PiL" starts with an overview of the Lua language and slowly evolves through the more advanced concepts and the features that make Lua such a unique language. Even though it's a relatively thin book, it goes from the starting concepts to the more sophisticated ones with a grace that is really rare to find. The one other book that comes to mind with an effect like this is the classical Structure and Interpretation of Computer Programs by Harold Abelson and Gerald Jay Sussman, which says a lot.

The first part of the book presents the language and the basic features. In particular, concepts such as iterators, coroutines and closures are presented in a clear way, which is no small feat considering that many readers will be having contact with such concepts for the first time.

One of the strong points of Lua is its simplicity, but don't assume that this implies lack of power. An example of this balance is the table data structure, in fact the only data structure for Lua. Tables allow the construction of every other data structure (arrays, sets, bags etc) but also allows the use of sophisticated techniques such as metamethods, function environments, modules and object orientation.

In fact, instead of providing every feature one might want to use, Lua provides the meta mechanisms so you can create your own features, or use them as modules created by third parties. In order to explain the full power of these meta mechanisms the second part of the book takes the reader from the basic steps of creating a table and shows how each of the mechanisms can add features or help the usage of some advanced concepts.

As an example, the chapter on object-oriented programming not only shows how to use objects in Lua, but also how to think of objects as prototypes for other objects, and how to implement inheritance on such a model without using anything too fancy.

There are also chapters on the use of metatables (basically tables that define the behavior of other tables), the use of tables as function environments (a really powerful mechanism once understood), modules and finally weak tables. Weak tables allow the advanced use of memory resources in an automatic way, providing mechanisms for data structures that tend to be hard to implement in other languages, if even possible at all.

This slow but efficient presentation of concepts that, though complex, can be easily assimilated by the reader is a predominant aspect of Roberto's work. He is not only a skilled author but also knows how to present things in a way that a novice can grasp and an expert can master.

The third part of Programming in Lua covers all the language libraries (math, table, string, system, debug and I/O) in clear prose and shows that even with a small set of libraries Lua can pack a lot of power on a very small footprint.

Finally, the last part of the book is directed to programmers that want to use Lua to its full potential. Lua can be used as an extension language or as an embedded language, and knowing how to interact with the host language (C, C++, Java, Delphi etc.) is mandatory for that. The book presents the C API for Lua in the same progressive pace, but also manages to cover every ground needed so one can add Lua to a project without much pain, if any.

One important thing to note about the book is that it follows the same philosophy that guides the language, showing that simplicity and lightness has nothing to do with lack of power or coverage.

I really recommend this book to anyone interested in learning Lua, and also for those who already know how to use Lua but would like to fully master its way of thinking, the so called "Lua way" of doing things. The book has been considered by many as an excellent guide on programming, even if one is not interested in Lua in particular."

You can purchase Programming in Lua, 2nd Edition from bn.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.

9 of 131 comments (clear)

  1. Perfect... by T.Louis · · Score: 2, Informative

    ... for the Burning Crusade expansion of World of Warcraft (Lua 5.1) and customized UIs!

  2. ref book online by Anonymous Coward · · Score: 5, Informative

    Every page has a donation or Amazon link...
    Anyways the ref book is online:
    http://www.lua.org/manual/5.1/

  3. Re:Just what the world needs by vashfish · · Score: 5, Informative

    A toy programming language used (possibly unwittingly) by at LEAST 7,000,000 people, thanks to World of Warcraft...

  4. Plua: Lua on a Palm Pilot by HoneyBeeSpace · · Score: 3, Informative

    If you want to geek out a bit and program Lua on your Palm, you may do so: http://netpage.em.com.br/mmand/plua.htm

  5. Celestia by Fallingcow · · Score: 4, Informative

    If you need a reason to learn Lua, look no farther than Celestia.

    Lua is the more powerful of the two scripting languages that can be used with Celestia. You can do some awesome stuff with it, and there are lots of examples on various forums (fora?) devoted to Celestia scripting. Good times.

  6. lua uses by Anonymous Coward · · Score: 3, Informative

    http://www.lua.org/uses.html list an impressive amount of project (many of them games) using lua.

  7. It is an excellent book by mhackarbie · · Score: 4, Informative
    I don't have any association with the author or the other creators of the language, so perhaps my opinion will add some legitimate support for the book and the language. I developed my previous project (Ribosome Builder) with Lua and found it to be very stable, easy-to-use, small, fast and powerful. That said, I was eagerly looking forward to the release of this next edition of the book, because I'm using Lua again for my current projects, and hope to make even better use of Lua 5.1. I'm especially excited about the new support for modules, and also the fact that Lua is now supported by SWIG. Previously, I had to do a lot of manual hacking to define the interface functions between my core code (in C++) and the lua scripts.

    I've read about 3/4 of the book so far and I completely agree with the reviewer's assessment. It's very cleanly and clearly written, with many things explained in a concise and elegant style. For example, Ierusalimschy's explanation of closures allowed me to immediately grasp them and appreciate why they are useful. I remember reading about them way back years ago in Larry Wall's book 'Programming Perl', and was remained rather confused about the concept. I don't know if the additional years of experience helped, but the clear style of the Lua book certainly did.

    Using a scripting language for enhancing and extending a complex project just seems to be a given for most serious projects these days, and after surveying the field, I considered only two main choices: Python and Lua. Python is also really well designed and powerful, but I decided to go with Lua because it does pretty much everything I need it to do, does it very well, but best of all, it is so very small. These days when even the most basic projects can quickly grow into complex, interconnected monstrosities with a zillion dependencies, I believe that the values of small and simple are more important than ever. So for that reason especially, I'm really excited about Lua and the prospect for using it more effectively after I finish swallowing this Blue PIL.

    mhack

    --
    Building a better ribosome since 1997
  8. Re:DOA - the license sucks. by Goaway · · Score: 2, Informative

    Wow, that's the most brain-washed open-source zealot response I've seen in a while. Do you even understand the "free as in beer" line at all? Apparently not.

    If it really, really, really bothers you that Lua doesn't have enough restrictions on distribution, you are quite free to download a copy, stick a GPL license on it, and use that. How about that?

  9. Re:data description language; Lua vs Guile by SimHacker · · Score: 2, Informative

    Absolutely: The Lua interpreter source code is very clean and well written, and wonderfully portable and platform agnostic.

    Here's the source code that you can view online -- there isn't much to it! Four global header files, 19 core C files, 19 core header files, 10 library C files, 1 interpreter C file, and 2 compiler C files. Here is the main loop of the virtual machine -- notice that there are only 38 opcodes!

    A great example of some interesting code written in Lua is the Auctioneer add-on for World of Warcraft (screenshots, manual). Here's the index of the Lua sources, and an interesing Lua file that calculates statistics on auction items. This code depends on features provided by the WOW client (implemented in C++ or whatever), as well as other Lua scripts loaded into the client.

    One important reason to learn and consider using Lua, is that it's by far one of the fastest and smallest of all the interpreted scripting languages, on the Programming Language Shootout. It totally smokes most other scripting languages.

    Here are the ratios of interpreted languages compared to compiled C code, in order of SPEED (the number is how many times slower it is than C, smaller is better unless you make your living by wasting time):
    Lua: 6.4; Python: 7.4; Pike: 8.3; Tcl: 8.7; Perl: 9.0; Scheme MzScheme: 11; PHP: 13; Icon: 14; Smalltalk GST: 15; Ruby: 16; JavaScript SpiderMonkey: 32;

    Here are the ratios of interpreted languages compared to compiled C code, in order of SIZE (the number is how many times bigger it is than C, smaller is better unless you make your living by selling memory):
    Lua: 2.5; Haskell GHC: 2.8; SML MLton: 3.4; Python: 4.1; Perl: 4.3; Tcl: 5.1; Icon: 5.4; Ruby: 6.0; C# Mono: 6.3; Pike: 6.8; PHP: 7.1; Oberon-2 OO2C: 7.9; Erlang HiPE: 7.9; Java JDK -server: 9.1; Scheme MzScheme: 9.2; Mozart/Oz: 9.8; Scala: 10; Lisp SBCL: 10; Smalltalk GST: 13; Smalltalk VisualWorks: 15; JavaScript SpiderMonkey: 30;

    Lua is even better than several compiled languages (like Java) when it comes to its size! Like Java, Lua also has a "just in time" compiler, but that was not used in these benchmarks (although I presume Java's was, because Java did very well with speed but not memory usage).

    I think it's laughable that someone would put their time into learning a faddishly popular language like Ruby, but would then not consider learning a technically superior language like Lua, since Ruby scores so badly on these benchmarks compared to Lua, Lua has been around a lot longer than Ruby, and it had already proven itself in many commercial products (like WOW).

    Lua really is far ahead of the pack of other languages in many ways, BECAUSE it's so clean and well designed. Plus its licensing terms are excellent, it's extremely portable, easy to embed and integrate with applications, and SWIG supports it well. So it's definitely well worth learning.

    -Don

    --
    Take a look and feel free: http://www.PieMenu.com