Cover image for Beginning Lua programming
Title:
Beginning Lua programming
Author:
Jung, Kurt, 1956-
Personal Author:
Publication Information:
Indianapolis, IN : Wiley/Wrox, [2007]

©2007
Physical Description:
xxviii, 644 pages : illustrations ; 24 cm.
General Note:
Includes index.

"Programmer to programmer"--Cover.
Language:
English
Added Author:
ISBN:
9780470069172
Format :
Book

Available:*

Library
Call Number
Material Type
Home Location
Status
Central Library QA76.73.L82 J96 2007 Adult Non-Fiction Non-Fiction Area
Searching...

On Order

Summary

Summary

This book is for students and professionals who are intrigued by the prospect of learning and using a powerful language that provides a rich infrastructure for creating programs. No programming knowledge is necessary to benefit from this book except for the section on Lua bindings, which requires some familiarity with the C programming language. A certain comfort level with command-line operations, text editing, and directory structures is assumed.

You need surprisingly little in the way of computer resources to learn and use Lua. This book focuses on Windows and Unix-like (including Linux) systems, but any operating system that supports a command shell should be suitable. You′ll need a text editor to prepare and save Lua scripts.

If you choose to extend Lua with libraries written in a programming language like C, you′ll need a suitable software development kit. Many of these kits are freely available on the Internet but, unlike Lua, they can consume prodigious amounts of disk space and memory.


Author Notes

Between his first programs submitted to a Burroughs 5500 on Hollerith punch cards and his latest programs tapped into a Palm Pilot, Kurt Jung has been the principal programmer on various projects ranging from airline yield management to state machine driven workflow.

Aaron Brown began programming in elementary school on a Commodore 64. He plays various musical instruments and speaks Esperanto.


Table of Contents

Acknowledgmentsp. ix
Introductionp. xxiii
Chapter 1 Getting Situatedp. 1
Choosing How to Install Luap. 1
Building Lua Yourselfp. 2
Selecting Prebuilt Luap. 3
Finding Your System's Shellp. 3
Windows Shellsp. 3
Shells on Unix and Unix-Like systemsp. 3
Shell Featuresp. 4
The Environmentp. 4
Environment Variables on Unix-Like Systemsp. 4
Environment Variables on Windowsp. 5
Dealing with Tarballs and Zip Filesp. 6
Compiling Luap. 7
The Lua Source Tarballp. 7
Compiling Lua on Linux and Other Unix-Like Systemsp. 8
Compiling Lua on Windowsp. 12
Building Lua with Microsoft Visual C++p. 13
Building Lua with the Tiny C Compilerp. 14
Building Lua with MinGWp. 16
Binary Packagesp. 13
Selecting a Prebuilt Binary Packagep. 18
Installing a Prebuilt Binary Package on a Unix-Type Systemp. 19
Installing a Prebuilt Binary Package on Windowsp. 20
Additional Toolsp. 21
Programmer's Editorp. 21
Revision Control Systemp. 22
Summaryp. 22
Chapter 2 First Stepsp. 23
Numbers and Arithmetic Operations: Basic Interpreter Usagep. 23
Addition, Subtraction, Multiplication, Division, and Exponentiationp. 24
Interacting with the Interpreterp. 24
Other Notations for Numbersp. 25
Interpreter Know-Howp. 26
Quitting the Interpreterp. 26
Interpreter Shortcutsp. 26
Numerical Gotchasp. 27
Division by Zero and Overflowp. 27
Floating-Point Roundingp. 28
Variables and Assignmentp. 28
Assignment Basicsp. 29
Multiple Assignmentp. 31
Variables on the Right Side of Assignmentsp. 32
Stringsp. 32
Quoting Stringsp. 32
Quoting Strings with Double Quotesp. 32
Quoting Strings with Single Quotesp. 33
Quoting Strings with Square Bracketsp. 33
Backslash Escapingp. 35
Relational Operators and Boolean Valuesp. 37
Comparing Numbersp. 37
Comparing Stringsp. 38
The nil Valuep. 40
Boolean Operatorsp. 41
The and Operatorp. 42
The or Operatorp. 43
The not Unary Operatorp. 44
The Concatenation, Length, and Modulo Operatorsp. 45
The String Concatenation Operatorp. 45
The Length Operatorp. 46
The Modulo Operatorp. 47
Automatic Conversion of Operandsp. 48
Precedence and Associativityp. 49
Variables and Valuesp. 51
Commentsp. 52
Expressions and Statementsp. 53
Compound Statementsp. 54
The if Statementp. 55
The while Loopp. 58
The for Loopp. 60
The repeat Loopp. 62
The break and do Statementsp. 63
Summaryp. 66
Exercisesp. 66
Chapter 3 Extending Lua with Functionsp. 69
Return Valuesp. 72
Using a Function that Returns a Valuep. 72
Defining a Function that Returns a Valuep. 73
Using return to Alter Control Flowp. 74
Returning Nothingp. 76
Returning Multiple Valuesp. 77
Adjusting Value Listsp. 78
Using Multiple-Valued Functions in Value Listsp. 78
Using Valueless Functions in Value Listsp. 79
Chunks as Functionsp. 81
Variable Scopep. 84
Actual and Formal Argumentsp. 84
Local Variablesp. 85
Understanding Side Effectsp. 91
Ordering Side Effectsp. 91
Short-Circuit Evaluationp. 93
Functions Calling Functionsp. 95
The Call Stackp. 95
Recursionp. 97
Stack Overflowp. 98
Tail Callsp. 99
Functions as Valuesp. 102
Replacing Built-In Functionsp. 102
Comparing and Printing Functionsp. 103
Function Definitions as Assignmentsp. 103
Local Functionsp. 105
Whitespace, Semicolons, and Function Callsp. 106
Upvalues and Closuresp. 108
Defining Functions that Create Functionsp. 108
Defining Functions with Private Statep. 110
Figuring Out Tricky Scope Situationsp. 111
Summaryp. 113
Exercisesp. 114
Chapter 4 Working with Tablesp. 117
Tables Introducedp. 117
A Shorter Way to Write Some Keysp. 119
Altering a Table's Contentsp. 120
Tables as Arraysp. 121
Array Lengthp. 123
Looping through Tablesp. 124
Tables of Functionsp. 128
The Table Libraryp. 128
table.sortp. 128
table.concatp. 131
table.removep. 132
table.maxnp. 132
Object-Oriented Programming with Tablesp. 133
Functions with Variable Numbers of Argumentsp. 136
Defining Vararg Functionsp. 136
Scripts as Vararg Functionsp. 140
Keyword Argumentsp. 143
Different but the Samep. 144
Table Equalityp. 144
Avoiding Bugs by Understanding Mutabilityp. 145
Variables and Mutable Valuesp. 145
Tables and Functionsp. 147
Copying Tablesp. 148
Building Other Data Structures from Tablesp. 152
Custom-Made Loopsp. 158
Global Variable Environmentsp. 163
Summaryp. 168
Exercisesp. 169
Chapter 5 Using Stringsp. 171
Basic String Conversion Functionsp. 171
String Lengthp. 173
Converting Between Characters and Character Codesp. 173
Formatting Strings and Numbers with string.formatp. 174
Input/Outputp. 180
Writing to and Reading from a Filep. 181
Pattern-Matchingp. 185
Searching for a Specific Stringp. 186
Matching Any of Several Charactersp. 186
Matches of Varying Lengthsp. 193
Capturesp. 198
Matching Balanced Delimitersp. 202
More on string.find, string.match, and string.gsubp. 202
Iterating Through All Matchesp. 204
Tricks for the Trickyp. 207
Magic Characters Chartp. 209
Summaryp. 210
Exercisesp. 210
Chapter 6 Handling and Avoiding Errorsp. 213
Kinds of Errorsp. 213
Syntax Errorsp. 213
Runtime Errorsp. 217
Handling Errorsp. 218
Default Error Behaviorp. 218
Checking Assumptionsp. 219
Code Errorsp. 220
Data Errorsp. 220
The assert and error Functionsp. 220
Defining Your Own Error Conditionp. 221
Anticipating Error Conditionsp. 222
Working with Return Valuesp. 222
Structuring Codep. 224
Error-Containment Functionsp. 227
The pcall Functionp. 227
The xpcall Functionp. 229
User-Written Scriptsp. 230
Locating Errorsp. 230
Summaryp. 230
Exercisesp. 231
Chapter 7 Using Modulesp. 233
Interfaces and Implementationsp. 233
The require Functionp. 234
Where to Put Modulesp. 235
Creating a Module Directoryp. 235
Setting Lua's Environment Variablep. 236
Preserving a Module's Interfacep. 236
Module Bookkeepingp. 240
Bytecodep. 241
Namespacesp. 242
Creating and Reusing Namespacesp. 242
Avoiding Global Variablesp. 244
Using the strict Modulep. 244
Reporting All Global Assignmentsp. 244
The module Fynctionp. 245
C Modulesp. 247
Summaryp. 247
Exercisesp. 247
Chapter 8 Extending Lua's Behavior with Metamethodsp. 249
Using Concatenation and Arithmetical Operators on Tablesp. 249
Relational Metamethodsp. 257
Indexing and Call Metamethodsp. 258
Non-Tables with Metamethodsp. 265
Non-Syntactical Metamethodsp. 267
Metamethod Applicabilityp. 268
Summaryp. 268
Exercisesp. 269
Chapter 9 Handling Events Naturally with Coroutinesp. 271
Coroutines and Program Controlp. 271
Coroutines Are Not Functionsp. 272
How Coroutines Are Like Programsp. 272
Coroutines Transfer Controlp. 273
Wrapping a Coroutinep. 273
Coroutines Are Cooperativep. 273
Outside Looking Inp. 275
Coroutines Have Statusp. 278
Rules of Conductp. 279
Work Shoulder-to-Shoulderp. 279
Trust the Dispatcherp. 280
Expect the Best, Prepare for the Worstp. 280
Play on Your Side of the Fencep. 280
Avoid the Deep Endp. 281
Managing Concurrent Tasksp. 281
Retaining Statep. 282
Exercising a Coroutine's Memoryp. 282
Iterating with Coroutinesp. 286
Handling Events Simplyp. 287
The Event Loopp. 288
Yielding to Another Coroutinep. 296
Summaryp. 297
Exercisesp. 297
Chapter 10 Looking Under the Hoodp. 299
Bytecode and luacp. 299
Garbage Collectionp. 303
The Implementation of Tables and Stringsp. 307
The Debug Libraryp. 308
Inspecting and Manipulating Running Codep. 308
Hooksp. 315
Other Functions in the Debug Libraryp. 321
Summaryp. 321
Exercisesp. 322
Chapter 11 Exploring Lua's Librariesp. 325
Core Libraryp. 325
Environment Functionsp. 325
Metatable Functionsp. 326
Chunk-Loading Functionsp. 328
Error-Containment Functionsp. 330
Module Functionsp. 331
The Garbage-Collection Functionp. 332
Type and Conversion Functionsp. 333
Basic Outputp. 333
Error-Condition Functionsp. 333
Table Traversal Functionsp. 334
Vararg-Related Functionsp. 335
Coroutine Libraryp. 336
Package Libraryp. 338
String Libraryp. 340
Pattern-Based String Functionsp. 340
String-Conversion Functionsp. 342
Table Libraryp. 344
Math Libraryp. 345
Trigonometric Functionsp. 345
Inverse Trigonometric Functionsp. 348
Hyperbolic Functionsp. 351
Exponent Functionsp. 354
Logarithm Functionsp. 356
Adjustment Functionsp. 358
Floating Point Representationp. 360
Angle Conversion Functionsp. 361
Pseudo-Random Number Functionsp. 362
Modulus Functionsp. 362
Minimum and Maximum Functionsp. 363
Constantsp. 363
Input/Output Libraryp. 364
Operating System Libraryp. 368
CPU Timingp. 368
Time and Date Functionsp. 368
Filesystem Functionsp. 369
Other Operating System Functionsp. 370
Debugging Libraryp. 370
Summaryp. 373
Chapter 12 Using Community Librariesp. 375
Library Overviewp. 375
Dynamically Linked Librariesp. 376
Resolving External Referencesp. 376
Configuration Optionsp. 376
Libraries Built from Source Codep. 377
Building Libraries on Unix-Like Systemsp. 378
Building Libraries on Windowsp. 378
Limits to Portabilityp. 379
How Lua Interacts with Librariesp. 379
The Variable Registration Processp. 379
Calling a C Function from Luap. 380
The pack Binary Structuring Libraryp. 383
Building the pack Library on Unix-type Systemsp. 383
Building and Installing the pack Library on Windowsp. 384
Testing the pack Libraryp. 384
Installing the pack Libraryp. 385
Using the pack Libraryp. 385
The cURL File Transfer Libraryp. 389
Building libcurlp. 389
Building libcurl on Unix-Like Systemsp. 390
Building libcurl on Windowsp. 391
Building luacurlp. 392
Building luacurl on Unix-Like Systemsp. 392
Building luacurl on Windowsp. 393
Using luacurlp. 393
The gd Graphics Libraryp. 395
Building gdp. 395
Building gd on Unix-Like Systemsp. 396
Installing gd on Windowsp. 396
Building lua-gdp. 397
Building lua-gd on Unix-Like Systemsp. 397
Building lua-gd on Windowsp. 398
Using lua-gdp. 399
The SQLite Database Libraryp. 405
Building SQLite3p. 405
Building SQLite3 on Unix-Like Systemsp. 405
Building SQLite3 on Windowsp. 406
Building lua-sqlite3p. 407
Building lua-sqlite3 on Unix-Like Systemsp. 407
Building lua-sqlite3 on Windowsp. 408
Using lua-sqlite3p. 409
Summaryp. 411
Exercisesp. 412
Chapter 13 Interfacing Lua with Other Languagesp. 413
How C Programs Use Luap. 413
Embedding Luap. 414
Extending Luap. 414
Embedding or Extending: Which Is Best?p. 414
Communicating Between Lua and Cp. 415
Calling Lua from Cp. 421
Obtaining a Lua Functionp. 421
Calling a Lua Functionp. 421
Protected Callsp. 422
Working with Userdatap. 423
Indexing Values in Cp. 436
Retrieving Indexed Valuesp. 436
Setting Indexed Valuesp. 437
Retaining Values in Cp. 438
The Registryp. 438
C Function Environmentsp. 439
Upvalues in Cp. 439
Referencing Valuesp. 440
The Thread Environmentp. 441
Layering Your Extension Libraryp. 441
Summaryp. 447
Exercisesp. 448
Chapter 14 Managing Information with Databasesp. 449
Some Basic Relational Database Conceptsp. 449
SQL, LuaSQL, and MySQLp. 458
Summaryp. 466
Exercisesp. 466
Chapter 15 Programming for the Webp. 467
A Web Server Primerp. 467
Dynamic Web Contentp. 468
Embedded Web Serverp. 468
Extended Web Serverp. 469
Creating Content at Run Time with Luap. 469
Executing CGI Scriptsp. 469
CGI Scripts on Unix-Type Systemsp. 470
CGI Scripts on Windowsp. 470
Installing a Web Serverp. 471
Apachep. 471
TinyWebp. 472
Testing Your Web Server with Static Contentp. 474
Serving Dynamic Web Contentp. 474
Problems with CGI Scriptsp. 475
Asynchronous Calls to the Serverp. 476
Producing a Calendar Dynamicallyp. 478
Producing Charts Dynamicallyp. 481
Interactive CGI Applicationsp. 489
CGI Helper Routinesp. 489
Developing CGI Scriptsp. 498
Security Issuesp. 498
The Kepler Projectp. 498
CGI the Kepler Wayp. 499
Lua Pagesp. 500
Summaryp. 501
Exercisesp. 501
Chapter 16 Connecting to a Larger Worldp. 503
Installing LuaSocketp. 503
Compiling LuaSocketp. 504
Compiling on Linux and Other Unix-Like Systemsp. 504
Compiling on Windowsp. 504
Installing Windows Binariesp. 505
Network Overviewp. 506
Routed Packetsp. 506
Addressesp. 507
Domain Namesp. 507
Identifying Internet Resourcesp. 508
Transport Protocolsp. 509
Sockets: Streams and Datagramsp. 510
TCP Socket Sociologyp. 511
Using LuaSocket for Network Communicationp. 512
Handling Multiple Persistent Connectionsp. 518
Using Lua Coroutines with the select Functionp. 518
Multiple Connections on the Server Sidep. 522
Setting Timeout Values for the Server Socketp. 523
The Application Protocolsp. 524
Filtering the Flow of Datap. 524
Accessing Web Pagesp. 527
Sending and Receiving E-mail Messagesp. 529
Networking with Lua and Streamsp. 536
On the Server Side: inetd and Friendsp. 536
On the Client Side: ssh and Friendsp. 538
Summaryp. 541
Exercisesp. 542
Chapter 17 Programming Games with Luap. 543
Understanding Why and When to Use Luap. 543
Simple 2-D Action Game Using SDLp. 544
Installing SDL and LuaCheiap. 544
Using SDLp. 546
Summaryp. 562
Exercisep. 562
Chapter 18 Carrying Lua with Youp. 565
Getting Started with Pluap. 565
Obtaining Pluap. 566
Examining the Distribution Contentsp. 566
Exploring Plua's Featuresp. 567
Running the Plua Applicationp. 567
Saving Plua Programsp. 569
Reading the Online Documentationp. 570
Using Palm OS Streamsp. 571
Compiling Applicationsp. 572
Compiling Librariesp. 573
Plua on the Mothershipp. 576
The Command-Line Compilerp. 576
The Palm OS Emulatorp. 577
Obtaining the Emulatorp. 577
Installing on Windowsp. 578
Configuring POSEp. 578
Running Plua in the Emulatorp. 578
Compiling a Program in the Emulatorp. 580
Exiting the Emulatorp. 580
The Palm OS Simulatorp. 581
Obtaining the Simulatorp. 581
Using the Simulatorp. 581
Programming with Pluap. 581
Generating Graphicsp. 582
Programming the User Interfacep. 583
Accessing Databasesp. 590
Summaryp. 592
Exercisesp. 593
Chapter 19 Fitting into the Lua Communityp. 595
The Lua Web Sitep. 596
The Lua Reference Manualp. 596
Framing Questionsp. 597
The Lua Mailing Listp. 597
Viewing and Searching the Archivesp. 597
Downloading the Archivesp. 598
Using a Web Browser to Access the Listp. 599
Using a Newsreader to Access the Listp. 599
Subscribing to the List Serverp. 599
Posting Messagesp. 600
The Lua Chat Roomp. 601
Forumsp. 601
The Lua Wikip. 601
LuaForgep. 602
Annual Workshopsp. 603
Summaryp. 603
Appendix A Answersp. 605
Indexp. 629

Google Preview