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.

6 of 131 comments (clear)

  1. Re:DOA - the license sucks. by planetoid · · Score: 4, Insightful

    How so? Last I checked, anyone is free to modify Lua to their heart's content and include the modified Lua VM in their program, and they can choose whether or not to release the source. That's even more free than what the GPL states, if you consider Lua's domain. Remember, Lua has been and is still used by many game developers who don't necessarily want modifications done to their games (or at least to their games' code, if not the data), and Lua would probably have failed in that market if it had a restrictive, bossy license like the GPL behind it. Lua is anything but DOA.

    --
    Slashdot requires you to wait longer between hitting 'reply' and submitting a comment.
  2. Re:Perfect... by alienmole · · Score: 5, Insightful
    Like text editors, it would be better to focus on fewer targets and add features

    The history of programming languages shows that this is a good way to kill a language. Languages are a delicate balance between enabling you to express things, and preventing you from expressing things. You can't just throw the kitchen sink in there and expect the result to be usable. People design new programming languages because they perceive limitations in the ones they already know, limitations that often can't just be overcome by adding features.

    From a programmer's point of view, a good reason to learn a new language is to expand your mental horizons. It can be a passable substitute for actually studying computer science (which you clearly haven't done, if you're asking "why another language?") Don't just learn an array of similar languages, like Python and Ruby. If you already know a popular OO language, then learn a language like Scheme, ML, or Erlang, or if you really want some nerdy metaphorical hair on your chest, learn Haskell (although you might want to start with one of the others first). Then you'll understand "why another language".

  3. Re:Perfect... by Cthefuture · · Score: 4, Insightful

    For starters Lua is older than Ruby so be sure to point your finger in the right direction. Lua was already at 1.0 when Matz starting thinking about Ruby.

    But Lua is different than those other languages anyway. It's extremely small, both as a language and as a binary. It's also easy to embed anywhere you need it. And finally it's pretty fast as far as dynamic languages go.

    Lua doesn't get enough credit these days. It's used in tons of products (especially games) but most of the time people don't realize they are using it.

    --
    The ratio of people to cake is too big
  4. PiL is the best language book I have ever read by raaum · · Score: 2, Insightful

    Even if you have no plans to actually program in Lua, you should get this book as an example of how a technical book can and should be written!

    While the novice programmer can certainly pick this book up and learn to program (and it is perfectly suited to that), the examples, asides, and discussion in each chapter contain gems for the intermediate to advanced programmer. Everytime I look something up I see a new depth to some language feature or example I didn't see the first time around.

    I use Lua because it is incredibly easy to both extend lua with either C or C++ libraries as well as embed lua in my C/C++/ObjC/whatever programs

  5. Re:languages as tools by Bastian · · Score: 2, Insightful

    I hate to burst your bubble, but if C++, Perl, Python, and Java are really such solid languages that there's no reason why any programmer would want anything else, then the world is screwed. Furthermore, adding new features to existing languages really isn't the solution - that path leads to madness, to kitchen-sink monstrosities like C++ and Perl. Both of them have traveled so far beyond the pale that their creators have admitted the desire to more or less start over and make a cleaner language that does the same thing.

    While I know this is a strained metaphor, when you're trying to do serious work a box of tools is better than a swiss army knife. That's part of the genius behind .Net and the reason why Mono started - with an environment where you can write different modules in different languages and not have to care quite so much about the boundaries between them, you really are free to use the best tool for the job. This is also why embedded languages are getting so popular - yeah, I can do everything I need to do in ObjC, but damn is it nice to be able to save myself a crapload of time by doing all the "big picture" work in something like Lua or F-Script. Nor do I find either to be an irritant - F-Script produced a drastic improvement in my productivity and the maintainability of my code, and now that I'm learning Lua I'm loving it for similar reasons. I'll probably keep using both because each is best for slightly different things.

  6. Re:As opposed to... by Raenex · · Score: 2, Insightful

    Who's "we", and when did they elect you to speak for them?