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.
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.
Disclaimer or not, should he be allowed as a reviewer?
Hmm, the Review Guidelines stateSo, it OK according to the guidelines. Though, i'd wonder if someone so involved is a good person to actually write such a review.
Have you read my journal today?
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.
Every time I see one of these "Learn Language X in Your Spare Time" book reviews, I wonder, "Why another language?" Advancement of languages is good, and "new" languages like Ruby are great, but how many programming languages are we as a community going to produce? Like text editors, it would be better to focus on fewer targets and add features than keep coming up with new "my toolbox grew so big I created a compiler for it" languages.
Of course, I may just be trollish today. Hard to tell.
The Spoon
Updated 6/28/2011
Every time I see one of these "Learn Language X in Your Spare Time" book reviews, I wonder, "Why another language?" Advancement of languages is good, and "new" languages like Ruby are great, but how many programming languages are we as a community going to produce? Like text editors, it would be better to focus on fewer targets and add features than keep coming up with new "my toolbox grew so big I created a compiler for it" languages.
I think that what you say really does apply to a number of languages -- python, ruby, php etc -- but lua really is something else -- small, elegant, and powerful, like scheme and C in their respective domains, but with some strikingly original constructs. Well worth the study.
The 'neat' thing about this language seems to be its easy memoization of functions. That would make it a good choice for programs that do certain kinds of calculations (and by the look of it, calculations that tend to be used in games).
Any of these 'new language X' are written to tackle a particular problem set. Would you use perl or C do to string munging? Would you use perl or Fortran to do fast and heavy number crunching? Being aware of what languages are out there may save you time when you encounter a particular problem.
I saw bring the new languages on. At the very least, their useful bits will be incorporated into a future perl release.
in brazil,from where this script language come from,lua = moon
Well, the first reaction of many people might be
"O no, yet another scripting language finds it way from the obscurity into the lame light". Do we need an extra one if we already have Ruby, Python, Perl, Tcl, Scheme. And I say -- YES, Lua has its place, it is not redundant, it is not "me too" language. And here is why.
I have been expert Ruby coder for the last 5 years using Ruby for data modelling, extensive scripting, wrote load-balancing scripts, Rails Web development, binding C++ libraries to Ruby using SWIG, you name it.
Six month ago I got involved in LUA and I totally fell in love with it.
What does make a beautiful programming language? Lots of features? wealth of libraries? simplicity of it? I think that language design is more art than science and the language beauty is the careful balance of features, simplicity, semantics, uniformity, etc. Like in a masterpiece painting it is the balance of color, shapes, motives and composition.
C, for example, ia a beautiful language in the category of "portable assemblers". In that category C is powerful thanks to its libraries, simple and easily implementable thanks to its syntax and semantics, portable due to very clever and clean hardware abstraction.
I think that Lua is to "high level scripting languages" is what C is to "portable assemblers". Lua has both OO and functional programming very naturally represented in its semantics. All objects are first class (including functions). Lua is small, very fast (in fact fastest scripting language according to http://shootout.alioth.debian.org/), has very good Virtual Machine, incremental Garbage Collector. As far as fundamantals are concerned, Lua is light-years ahead of Ruby. It still lags behind in library support, but the recent progress is very encouraging.
Anyway, give Lua a try. You will love it. Lua is nice, its codebase is tiny (about 10K lines). It runs on anything that support ANSI C compiler including embedded stuff (ARM, Palm, Cell phones, MIPS, x86, etc).
Actually a really elegant embeddable language. On previous products I've created I've embbbeded TCL, Python, LISP and FORTH. Lua is more elegant and almost as small as a FORTH kernel. I assure you Lua is already in at least three very propular products.
I have to admit that I don't know from shineola a thing about Lua. The book review didn't do anything to compel me to find out more, though, and that's unfortunate if Lua is such a wonderful thing to behold. It's not necessarily the job of a writer of a book review to educate his readership about the subject of the book in question, but a short paragraph telling me in a few sentences why I should care about Lua might make me more susceptible to his argument that it's something I should rush right out and buy. If I don't care enough about the language by the time I get to the end of the review, I'm not going to put enough effort into finding out more to even Google it.
.NET, VB has arrived at being a clean and useful language). I'm interested in Ruby because it's more cleanly object oriented than Perl, but I'm not going to put a bunch of effort into learning it because I don't see that it really adds anything to my capablities.
In my too-many years of being an IT professional, I've never been much of a programmer. I've dabbled with several languages, but left the serious work up to those with enough time on their hands to do it right. What I, as someone who spends more time gluing disparate applications together than developing new ones wants is a language that does the things I need it to today and will grow with the changing requirements of my job and information technology in general. Perl is great for a huge number of things, and a lot of what I do I choose Perl for. But give me a requirement to cobble together a GUI-based app, and I'm going to use VB.NET (I have to say that I feel like with
I guess I keep reading these book reviews in hopes that one day one will say, "Here's the language you need: It scripts well, compiles into binaries that are portable across platforms, produces GUI-based apps that bolt easily to databases, and is fast to code in. Oh, and this book tells you all about it in an easy-to-read format that will have you coding basic apps quickly and carry you through advanced concepts. Oh, yeah, and it's written assuming that the reader doesn't need to know what a variable is and how you can use one."
I suppose it's a lot to ask. I'll keep reading, though. And I'm going to look into Perl 6. Yeah, that too.
The Spoon
Updated 6/28/2011
I think Lua's main selling point is ease of embedding. When you write a large C++ application, you frequently end up wanting some embedded scripting language.
Some interpreters, like Perl, are tricky to embed. Lua is very friendly to embedding. You can call user-defined Lua functions from C and call C functions from Lua.
The interpreter is small, and the language is sandboxable - for instance, you could have untrusted Lua running in your webserver and it would have no access to I/O or filesystem.
No it's
print ("Hello, world.")
Reader's Digest guide to Lua:
-1) I believe it's 'Lua' not LUA
0) You need the parentheses on function calls.
a) Only data structure is hash tables
b) Hash tables with positive integer indexes emulate arrays (order is maintained)
c) Positive integer indexed hash tables are one-based. Gawd. This makes for some serious unpleasantness if you want to embed Lua in your C application, since many real world things are 0 based and probably all the patterns you know for writing proper loops in C are zero based. They wanted to make things simple, but they got this one dead wrong. Someone should make a zero-based fork.
d) No continue statement for loops
e) No semicolon separator
f) Iterators
g) Cool cooperative multitasking support
h) Closures
i) An extremely quirky stack based interface to C. Some api's pop their arguments some don't. They should do it the Forth way (words consume their arguments) or not automatically pop anything. Pick one, not both. And please, some simple stack operators (swap, drop, dup, roll, etc.)
j) Perlish do-it-yourself objects
k) Not-quite-regex string processing support. Come on, at least make it a subset of proper RE's, don't put a new syntax.
l) Implicit dereferencing
I'm probably missing something...
-- John.