Programming in Lua (2nd Edition)
Format: PDF / Kindle (mobi) / ePub
Lua is the language of choice for anyone who needs a scripting language that is simple, efficient, extensible, portable, and free. Currently, Lua is being used in areas ranging from embedded systems to Web development and is widely spread in the game industry, where knowledge of Lua is an indisputable asset. "Programming in Lua" is the official book about the language, giving a solid base for any programmer who wants to use Lua. Authored by Roberto Ierusalimschy, the chief architect of the language, it covers all aspects of Lua 5---from the basics to its API with C---explaining how to make good use of its features and giving numerous code examples. "Programming in Lua" is targeted at people with some programming background, but does not assume any prior knowledge about Lua or other scripting languages. This Second Edition updates the text to Lua 5.1 and brings substantial new material, including numerous new examples, a detailed explanation of the new module system, and two new chapters centered on multiple states and garbage collection.
such loops, we have ❢♦r ✐ ❂ ✶✱ ❢✭①✮ ❞♦ ♣r✐♥t✭✐✮ ❡♥❞ ❢♦r ✐ ❂ ✶✵✱ ✶✱ ✲✶ ❞♦ ♣r✐♥t✭✐✮ ❡♥❞ If you want a loop without an upper limit, you can use the constant ♠❛t❤✳❤✉❣❡: ❢♦r ✐ ❂ ✶✱ ♠❛t❤✳❤✉❣❡ ❞♦ ✐❢ ✭✵✳✸✯✐❫✸ ✲ ✷✵✯✐❫✷ ✲ ✺✵✵ ❃❂ ✵✮ t❤❡♥ ♣r✐♥t✭✐✮ ❜r❡❛❦ ❡♥❞ ❡♥❞ The for loop has some subtleties that you should learn in order to make good use of it. First, all three expressions are evaluated once, before the loop starts. For instance, in our first example, Lua calls ❢✭①✮ only once. Second, the control
frequently embedded in an application, it cannot simply crash or exit when an error happens. Instead, whenever an error occurs, Lua ends the current chunk and returns to the application. Any unexpected condition that Lua encounters raises an error. Errors occur when you (that is, your program) try to add values that are not numbers, call values that are not functions, index values that are not tables, and so on. (You can modify this behavior using metatables, as we will see later.) You can also
✐♦✳r❡❛❞✭✧✯❛✧✮ option, which reads the file at once. However, sometimes we must face this problem. Java offers the structure ❙tr✐♥❣❇✉❢❢❡r to ameliorate the problem. In Lua, we can use a table as the string buffer. The key to this approach is the t❛❜❧❡✳❝♦♥❝❛t function, which returns the concatenation of all the strings of a given list. Using ❝♦♥❝❛t, we can write our previous loop as follows: ❧♦❝❛❧ t ❂ ④⑥ ❢♦r ❧✐♥❡ ✐♥ ✐♦✳❧✐♥❡s✭✮ ❞♦ t❬★t ✰ ✶❪ ❂ ❧✐♥❡ ✳✳ ✧❭♥✧ ❡♥❞ ❧♦❝❛❧ s ❂ t❛❜❧❡✳❝♦♥❝❛t✭t✮ This
The call to str✐♥❣✳❣♠❛t❝❤ creates an iterator to traverse all occurrences of the pattern ‘❪❂✯❪’ (that is, a closing square bracket followed by a sequence of zero or more equal signs followed by another closing square bracket) in the string s.3 For each occurrence, the loop updates ♥ with the maximum number of equal signs so far. After the loop, we use str✐♥❣✳r❡♣ to replicate an equal sign ♥ ✰ ✶ times, which is one more than the maximum occurring in the string. Finally, str✐♥❣✳❢♦r♠❛t encloses s
control. Although programmers do not use this implementation frequently, it is instructive to know about it, both because it explores some interesting aspects of Lua and because it can be a good solution for other problems. The basic idea of this alternative design is to represent each object through two tables: one for its state and another for its operations, or its interface. The object itself is accessed through the second table, that is, through the operations that compose its interface. To