Beginning Lua Programming 173
Simon P. Chappell writes "These days, Brazil is no longer just on the map for its fine coffee and martial arts. They have a few other reasons and one of them is called Lua. Lua is an open-source scripting language that was originally designed for small size and excellent portability to support its use in embedded systems. Like anything good, it didn't take long for it to be discovered by others looking for a small and efficient scripting language."
Beginning Lua Programming | |
author | Kurt Jung, Aaron Brown |
pages | 400 |
publisher | Wiley |
rating | 9 |
reviewer | Simon P. Chappell |
ISBN | 0470069171 |
summary | A great introduction to Lua with solid writing and explanations of the concepts |
This is definitely a book targeted at those new to Lua. While it begins gently, it picks up speed quite quickly and takes its reader through a full range of Lua usage. This book is unusual, for the more mass-market beginner books, in that it does not treat the reader like an idiot. I found the tone to be very respectful of the reader's intelligence. This aspect alone, makes the book stand out in a crowded beginners market.
This book is also a very good introduction to Lua for programmers experienced with one or more other programming languages. I found it very easy to pick up the concepts with my previous programming experience.
The first chapter of most books like this, will typically cover getting setup with the language. This book delivers well, with not only explaining how to install the binary version, but also covers compilation under Unix-styled operating systems and a number of development environments for Microsoft Windows. Guidelines are given for building Lua on Windows using Visual C++, Tiny C and MinGW.
Chapter two is called "First Steps" and is a walkthrough of all of the core language. This is well done and there are plenty of examples where a Lua command is typed and the resulting display from the Lua environment is shown. Chapter three is dedicated to the art of the Lua function. Functions are first class values and have enough power and complexity that they deserve their own chapter. Did you know that Lua automatically recognises tail recursion and handles it appropriately? It does. So if your looking to experiment with some of the trendy functional techniques that tend to swamp Reddit these days, Lua would be a good place to try it.
Chapter four explores Lua's only data structure, the table. What's a table, you ask? My best description, as far as I've managed to get my head around it, would be that it's a Swiss-Army Knife style Hash Table with multiple personalities, alternatively thinking that it's an array, a list, an Erlang-style tuple or a tree structure. However you decide to use it, this chapter explains it and provides lots of examples. Chapter five brings us that ultimate datatype, the string. As with tables, Lua can do many things with the humble string and this chapter will make sure that you can work with substrings, handle user input, read and write files and use the built-in pattern matching available in the language.
Chapter six covers "Handling and Avoiding Errors". I know, I know, we don't have errors anymore, now that we've all discovered the benefits of agile development using scripting languages. Except that we do and circumstances external to our programs can still bring pain to our code. Users type the wrong things, network connections die, power can go out suddenly and, of course, sometimes there are parts of our programs that aren't as imperfect as we'd like to think they are. Chapter seven is about "Using Modules" and explains Lua's interfaces and implementations together with guidelines on modularising scripts and calling those modules.
Any agile language worth its electrons is going to have meta-programming facilities within it. Lua has them and chapter eight is where we learn about "Extending Lua's Behaviour with Metamethods". Chapter nine examines Lua's ability to handle events using coroutines. This is a powerful feature and enables Lua applications to manage concurrent tasks with ease.
Chapter ten explores the fascinating world of Lua internals. While Lua is a scripting language, it does use a virtual machine, this chapter shows how scripts can be pre-compiled to the appropriate bytecode. It also covers memory management and implementation details for tables and strings. Chapter eleven is somewhat reference-like, covering the standard libraries supplied with Lua. Chapter twelve is a good compliment, describing some of the available libraries from the Lua community.
Chapter thirteen addresses the matter of interfacing Lua with other languages. If your language can handle C-style calling conventions, then it can be used by your Lua programs. Chapter fourteen is for everyone that wishes to store information in a database now and then. Lua has bindings for connecting to a number of popular database systems and this chapter will help you use them.
My favourite chapter is number fifteen, where using Lua for web applications is explained. There are several alternatives for the budding Lua web application programmer, ranging from that humble and faithful technique, the CGI script, to the Kepler web server environment with the convenience of LuaPages. In this highly-connected world, it is important to be able to communicate over a network and if chapter fifteen didn't tip you off, then chapter sixteen will make you absolutely certain that Lua is ready for networks. Chapter sixteen explores networking from LuaSockets to email and webpages.
Lua is very popular among video games designers. While not generally used to write the frameworks, it is often used for in-game scripting. Chapter seventeen recounts many of the ways that Lua can be used within video games. Chapter eighteen will appeal to the Palm carrying geeks in the house. If you've been looking for a programming language for your Palm PDA, then Plua may be just what you were looking for. Plua (actually Plua2, but they call it Plua) is an implementation of Lua 5.0 for the Palm OS. It gives full access to both data and graphics and is programmable on the Palm; no need to be tethered. Finally, chapter nineteen addresses the subject of "Fitting into the Lua Community".
That's a long list of chapters. Phew! After all of that, let's see what was worth noting along the way.
The writing style is good. Light without being flippant and respectful of the readers intelligence. I like that. I liked the fact that the topics were broken down into appropriately sized chapters. It meant that this book has more chapters than usual, but each one is nicely partitioned, so it doesn't feel over-whelming. I also like the fact that this book seems like it will continue to be useful in the medium term as well. Some beginners books stop being useful quite quickly after you've learned the basics from them, but this one covers quite a number of topics that will useful at the mid-level of Lua skills.
I have very little negative to say about the book. While I normally don't care for books much larger than 300 pages, this one doesn't feel like it has any gratuitous stuffing in it. There is an appendix with answers to the programming questions asked here and there in the chapters, but other than that, everything feels like it should be there.
As a typography and layout nut, I did feel that the layout was a little cramped and the primary type size was a little small. The program listings were generally well done, although I'm not sure that every single one needed a gray background to help separate it from the body text.
The title of chapter nineteen, "Fitting into the Lua Community" seemed a little patronizing to me. I understand the need for maintaining community values, but most communities do that fairly well without the aid of authors playing the heavy. Apparently the community lasted this long without the book. Let's hope they make it a little longer, now that the masses know about it.
All in all, this is a great introduction to Lua. It has solid writing and explanations of the concepts, with plenty of examples. Recommended.
You can purchase Beginning Lua Programming from amazon.com. Slashdot welcomes readers' book reviews -- to see your own review here, read the book review guidelines, then visit the submission page.
LUA in WoW (Score:5, Informative)
WoW (Score:2, Informative)
Try out lua - on Nmap (Score:4, Informative)
With lua and NSE, nmap can now do things like vulnerability testing. Why not download nmap [insecure.org] now and give it a try?
Supreme Commander (Score:3, Informative)
Re:Another scripting language? (Score:4, Informative)
I must agree though that this article didn't do too much in terms of selling the language, which is probably what the book is supposed to do.
I'm sure I'm not the only one to point it out, but (Score:3, Informative)
What's with the "base index of 1" array conventions in their standard library?
I love Lua, and have played around with it quite a bit, but that part of their specs always annoyed me. Interestingly, it is flexible enough to allow custom "standard lib" code that uses base index of zero. Also, custom scriptable objects can define their own conventions, including relying more on the iterator interfaces, which forces things to be theoretically cleaner.
But still, just having that even as a possibility irks me.
Re:Another scripting language? (Score:2, Informative)
Re:Is it only for extending things? (Score:2, Informative)
when they say "lightweight"... they mean it (Score:5, Informative)
Should you use it for your online web application backend, or your system administration scripts? Probably not.
LUA in games (Score:2, Informative)
Lua works on Lego robots (Score:3, Informative)
Java does too http://lejos.sourceforge.net/ [sourceforge.net]
I don't think there are python or ruby ports though, possibly because these devices are very limited.
Re:I'm sure I'm not the only one to point it out, (Score:4, Informative)
Re:LUA in games (Score:1, Informative)
When asked how to get ready for the upcoming Lightroom SDK, an Adobe engineer said "Go hack WoW and come back when we have the SDK ready."
Some thoughts on Lua (Score:2, Informative)
I got on the Lua bandwagon last year, and for what it is designed to do, it is quite good.
Before I start cheerleading, some comments on the book. Beginning Lua Programming (BLP) is a very good book on the subject, especially for people new to Lua or perhaps even to programming. I own this book as well as the "Programming in Lua" (PIL) book by Roberto Ierusalimschy, the latter of which I think has some of the best technical writing I've seen in a computer book and is on par with the likes of the late W. Richard Stevens. The two books are both very good, but I think serve slightly different audiences. BLP is a comprehensive book filled with gradual, well thought-out examples that can pretty much lead anyone through Lua regardless of programming experience, and help them to learn it well. The PIL book is written for more experienced/professional programmers who don't need as much hand holding.
Despite being a small language, Lua sports some serious language features, and you will be sorry not to learn them all. This is where the two books differ I think. PIL assumes at least a nodding acquaintance with concepts such as closures, iterators, generators, and more advanced concepts such as its coroutines (which are a somewhat extended form of blocks in Ruby). If you are not familiar with these, BLP will ensure that you come away with a full understanding through a battery of examples and patient prose.
That said, people who are already familiar with the above (through prior experience with languages such as Ruby or Python, for example) might find BLP a little slow. This is where PIL really shines. PIL is an extremely well-written book by the Lua's principle architect which provides a full treatment of the language in an economy of pages. It is hard to believe when reading the text that English is the author's second language. The examples are all very good, well chosen, but not excessive. The book moves at a steady pace and covers the same ground in much less space than BLP. One place where PIL really shines is on covering the C API and C programming aspects of Lua. In this respect, it is clearly superior to BLP. But again, you have to be a well-grounded C/C++ developer to appreciate this.
As I said, I own them both. I read PIL first which got me embedding Lua in C applications in no time, and then went back and read BLP to round out and sharpen my skills in the language proper. I think the books can complement each other in this way. They are both above average books.
And now for those who have not used Lua and wonder why they would want to. Lua is a tiny, standalone language/interpreter that can be put almost anywhere. It is an ideal way to extend your application as the entire language and libraries fit into a single library under 200Kb in size. It is written in ANSI C and is extremely portable. In my opinion, Lua is not a language you would choose instead of Ruby or Python (unless of course you were already very proficient at it), as I think those languages have much more to offer in either syntactical elegance or extensive standard libraries. Lua's function as a language is very similar to SQLite's function as a database. It's a powerful little multitool you can use to dramatically enhance and extend your applications with very little effort. A feature that might take 3,000 lines of C might take 10 lines of Lua. My experience with it so far has been very positive, and it has pretty much become a staple in my programming repertoire. It is the ultimate compact domain specific language.
Re:I'm sure I'm not the only one to point it out, (Score:1, Informative)
Tell that to the modulo function. Or pretty much _any_ kind of index arithmetic. Would you rather do 28*i+7*j+k to do a mapping of threedimensional indices to one-dimensional, or 28*i+7*j+k-35? What about backwards?
k = n mod 7
j = (n div 7) mod 4
i = n div 28
or
k = 1+((n-1) mod 7)
j = (((n-1) div 7) mod 4) + 1
i = ((n-1) div 28) + 1
?
Some examples of what it's good for (Score:4, Informative)
It's also extremely small in terms of memory size and footprint, making it ideal for some embedded applications. I've used it to create a (yes, really) AJAX-enabled web-UI for configuring small Linux boards.
I had to write some sqlite bindings for Lua to do it (sorry, closed source, GRRR) but it really wasn't even remotely hard.
On the downside, most Lua projects start to feel like Lisp as you start to add your own language support for things that aren't in the core, and it's best to avoid the object/module support -- that's rather hairy stuff.
The problem with lua... (Score:1, Informative)
If the developers could at least maintain ABI stability so library bindings work between versions then we'd see lua gain serious traction outside of gamedev. The luajit gives mono a run for it's money in shootouts, [debian.org] and register based VMs are better suited to dynamic languages. [debian.org]
Re:when they say "lightweight"... they mean it (Score:3, Informative)
Did you just get modded up for not being able to type "MIT license" into google?
MIT is BSD-style without any clause that would stop it from being re-used in a commercial product. And sorry guys, this is a requirement for use in games right now.
Aircraft? (Score:3, Informative)
One should add to that that Brazil has one of the largest aircraft manufacturers in the world [embraer.com]. Embraer started in 1969 as a federal government owned company and was privatized in 1994. Although they had some technology transfer contracts with foreign companies in the past, their current lineup of airplanes [embraer.com] has been totally developed in Brazil.