Unix Review > Archives > 2004 > May 2004
Print-Friendly Version

May 2004

Regular Expressions: Lua Shines

by Cameron Laird and Kathryn Soraiz

Take a look a Lua.

We write that every few years (most recently in May of 2002) because the Lua programming language is underappreciated, and because it continues to advance despite the comparatively small number of people working on and with it.

This spring, though, is a particularly good time to jump to Lua (the Portuguese word for "moon"), because Version 5.0 and The Lua Book are both better than we expected them to be.

Simple and extensible

Let's begin with a bit of background. Lua is a high-level or "scripting" procedural language, roughly comparable to Tcl, Python, Ruby, and similar languages. Lua source code has a familiar appearance; the canonical factorial definition with which the book begins looks almost like:

 -- This is a comment.
 function factorial(n)
          if n == 0 then
             return 1
          else
             return n * fact(n - 1)
          end
 end
Lua is around a decade old; Version 1.1 was the first public release in July of 1994. It's used mostly outside the United
States, with the core implementation team based in Brazil. Although Lua is far down the list of computing languages in wide use in the United States, at least a couple of development communities strong in the United States have recognized the language's virtues; a poll at GameDev.net, for example, yielded Lua as the most popular language for game scripting.

Game authors and other fans prize Lua over similar languages for its efficiency, simplicity, extensibility, and portability. While its syntax and semantics are much like those of other languages, with a few exceptions we'll mention below, its architecture and implementation are quite distinct. Lua is designed to be small and simple, and to "play well" with other technologies, values "Regular Expressions" touts monthly. Although Lua supports object-orientation quite satisfyingly, for example, the Lua approach to a big project is not, "How can the language support this 100,000-line task?", nearly as much as it is, "How can the language meet all the project goals in 2,000 lines?" Lua has a relatively weak presence as a standalone process; it appears to be used most in embedded contexts. Game developers, for instance, might build C-coded libraries of specialized display and algorithmic functions, then embed the Lua library as a convenient way to configure and manage the application end users see.

All this has been true since the mid-90s. During most of that time, too, a frequent topic of Lua-oriented conversation was, "where is the Lua book?" There's now a good answer to that question.

The book

The hazard we feel in writing about Programming in Lua (PIL) is that we'll sound condescending or biased or both. We looked forward to its publication in December 2003 because we've crossed paths with its author, Roberto Ierusalimschy, often enough to expect good things. The surprise was in just how good the book is.

Ierusalimschy, who heads the Lua development team and is a full-time professor at the Pontifical Catholic University of Rio de Janeiro, tried to work with major computing industry publishers. They were unencouraging. He finally decided to publish the book himself. The result is remarkable.

If PIL had just collected online references in a definitive, accurately indexed dead-trees version, it would have been a welcome volume. In fact, the prose is of high quality, far better than a reworked "man page" or help file, and its 288 pages include valuable discussions of topics covered almost nowhere else: industrial-strength metaprogramming and the architectures of extension and embedding. These, and its grown-up presentations of object orientation, concurrency models, and introspection, are so meaty that we recommend PIL to any intermediate or advanced developer. Even those indifferent to Lua as a development language will find the book presents so many good ideas, and presents them so artfully, that it's worth reading if only with the attitude that Lua is an idiosyncratic pseudocode.

PIL reads so unusually smoothly as to make one wonder whether being a native speaker of English and editorial support from a major publisher are somehow disqualifications for clear writing. The explanations are consistently graceful and concise, with the word order of "... it means probably something wrong in our program", on page 63, the only noticeable lapse from a fully idiomatic rendering.

Complementing the delights of PIL are the progress of version 5.0, which it documents. 5.0 includes co-routines, tail calls, booleans, "weak tables", improvements in exception handling, lexical scoping, and metatables. A quick example should give a bit of insight into Lua style, and how the book and the latest release support it.

Programming with metatables

Tables are important to Lua. Tables — dictionaries or associative arrays, mostly — are the language's central data structure. Part of the language's simplicity is that tables support only a few operations, including iteration through a table's contents, adding or removing key-value pairs, and so on.

One of the reasons Lua can be simple, yet still effective, is the way it leverages metaprogramming to achieve more subtle effects. Chapter 13 is on "Metatables and Metamethods". The first extended example there shows not only how to define a Set from mathematical set theory, supported by such operations as Set.union and Set.intersection, but also how to "overload" the addition symbol so that Set1 + Set2 is properly calculated as the union of these two sets. This requires only a couple of incremental lines of implementation:

 function Set.new (t)
   local set = {}
     -- The next one is new.
   setmetatable(set, Set.mt)
   for _, 1 in $ipars(t) do set[l] = true end
   return set
 end
and
 Set.mt.__add = Set.union

This is typical of good Lua coding in a couple of ways: not only does it achieve a powerful effect with small means, but it does so thriftily, in that Lua is still the fastest, or at least one of the fastest, "scripting languages", and Lua also retains its portability. All the new 5.0 features haven't impaired Lua's capacity to port easily to essentially any platform which supports ANSI C.

Our conclusion: you should get a copy of PIL. Read it; you're likely to understand quite a bit more about mixed-language programming, concurrency, and embedding, whatever your chosen languages. If you also happen to need a particularly lightweight extension language, and especially if you need one which performs well, the answer is likely to be at hand.

Administrative matters

Thanks for your patience. "Regular Expressions" has been on vacation much of the last six months. We have a mountainous backlog of good computing ideas that deserve coverage, and we're looking forward to a return to our usual monthly schedule for bringing them to you. Next month brings an experiment which dramatically illustrates how good scripting can get in scientific programming.

Kathryn and Cameron run their own consultancy, Phaseit, Inc., specializing in high-reliability and high-performance applications managed by high-level languages. Join them each month as their "Regular Expressions" column explores issues and opportunities that arise in practical application development with scripting languages.

Sys Admin Spotlight

CMP DevNet Spotlight

Regular Expressions: Two Easy Steps Better Than One Hard One
Complicated regular expressions and yacc are powerful parsing tools, but they can cause trouble in inexperienced hands. One helpful alternative is "partial evaluation" (PE) or "active data" parsing

In the News

Windows Server 2008 RC1 Available For Download
Microsoft will launch the final version of the server OS at a February 27th event in Los Angeles.


Google Revamps iPhone Interface
Google's iPhone home screen makes services such as Gmail, Calendar, and Reader more accessible through the use of Ajax menu tabs.


CradlePoint Offers Personal HotSpot To Go

CradlePoint today announced a new personal Wi-Fi hotspot product that you can carry with you everywhere you go. Have hotspot, will travel.


Mom Faces Massive File-Sharing Fines After DOJ Sides With RIAA
Justice Department prosecutors argue that Jammie Thomas' $222,000 judgment is in line with the U.S. Copyright Act law.


France Telecom Sells 30,000 iPhones, Some Unlocked
Orange expects to sell up to 100,000 iPhones by the end of the year and between 400,000 and 500,000 by the end of 2008.


Palm Refreshes Treo 750 With Windows Mobile 6
The update includes the ability to send and receive e-mails formatted in HTML with tables, bullets, and colored text.


Microsoft Turns To Inkblots For Password Generation
The image associations are not only unique to the user, they're also "hard to forget," the researchers said.


CD-ROM

Sys Admin and The Perl Journal CD-ROM version 11.0

Version 11.0 delivers every issue of Sys Admin from 1992 through 2005 and every issue of The Perl Journal from 1996-2002 in one convenient CD-ROM!

Order now!




MarketPlace

Easy & Powerful Server Monitoring that Just Works
Fortune 500 clients include Financial, Healthcare & Telco Companies. Free Trial Download.

Government (GSA) Contract Services
Expand into a $43 billion market today. Government (GSA) contract assistance, $4800 fee. BBB member.

Discover WinDev 11 RAD
and develop 10 times faster ! ALM, IDE, .Net, PDF, 5GL, Database, 64-bit, etc. Free Express version

We Buy & Sell Used Cisco
Hula Networks is overstocked on many items including, used Cisco, Juniper, Foundry and Extreme networking equipment and can therefore offer outstanding pricing. We buy Cisco and sell Cisco networking equipment

Wanna see your ad here?