Beginning Lua Programming 9780470069172, 0470069171

Lua offers a wide range of features that you can utilize to support and enhance your applications. With this book as you

301 7 3MB

English Pages 673 Year 2007

Report DMCA / Copyright

DOWNLOAD PDF FILE

Table of contents :
Beginning Lua Programming......Page 1
Acknowledgments......Page 10
Contents......Page 12
The Facets of Lua......Page 24
Lua Is Free and Open......Page 25
What You Need to Use This Book......Page 26
Conventions......Page 27
p2p.wrox.com......Page 28
Choosing How to Install Lua......Page 30
Building Lua Yourself......Page 31
Shells on Unix and Unix-Like systems......Page 32
The Environment......Page 33
Dealing with Tarballs and Zip Files......Page 35
The Lua Source Tarball......Page 36
Compiling Lua on Linux and Other Unix-Like Systems......Page 37
Compiling Lua on Windows......Page 41
Selecting a Prebuilt Binary Package......Page 47
Installing a Prebuilt Binary Package on a Unix-Type System......Page 48
Installing a Prebuilt Binary Package on Windows......Page 49
Programmer’s Editor......Page 50
Summary......Page 51
Numbers and Arithmetic Operations: Basic Interpreter Usage......Page 52
Interacting with the Interpreter......Page 53
Other Notations for Numbers......Page 54
Interpreter Shortcuts......Page 55
Division by Zero and Overflow......Page 56
Variables and Assignment......Page 57
Assignment Basics......Page 58
Multiple Assignment......Page 60
Quoting Strings......Page 61
Backslash Escaping......Page 64
Comparing Numbers......Page 66
Comparing Strings......Page 67
The nil Value......Page 69
Boolean Operators......Page 70
The and Operator......Page 71
The or Operator......Page 72
The not Unary Operator......Page 73
The String Concatenation Operator......Page 74
The Length Operator......Page 75
The Modulo Operator......Page 76
Automatic Conversion of Operands......Page 77
Precedence and Associativity......Page 78
Variables and Values......Page 80
Comments......Page 81
Expressions and Statements......Page 82
Compound Statements......Page 83
The if Statement......Page 84
The while Loop......Page 87
The for Loop......Page 89
The repeat Loop......Page 91
The break and do Statements......Page 92
Exercises......Page 95
Extending Lua with Functions......Page 98
Using a Function that Returns a Value......Page 101
Defining a Function that Returns a Value......Page 102
Using return to Alter Control Flow......Page 103
Returning Nothing......Page 105
Returning Multiple Values......Page 106
Adjusting Value Lists......Page 107
Chunks as Functions......Page 110
Actual and Formal Arguments......Page 113
Local Variables......Page 114
Ordering Side Effects......Page 120
Short-Circuit Evaluation......Page 122
The Call Stack......Page 124
Recursion......Page 126
Stack Overflow......Page 127
Tail Calls......Page 128
Replacing Built-In Functions......Page 131
Function Definitions as Assignments......Page 132
Local Functions......Page 134
Whitespace, Semicolons, and Function Calls......Page 135
Defining Functions that Create Functions......Page 137
Defining Functions with Private State......Page 139
Figuring Out Tricky Scope Situations......Page 140
Summary......Page 142
Exercises......Page 143
Tables Introduced......Page 146
A Shorter Way to Write Some Keys......Page 148
Altering a Table’s Contents......Page 149
Tables as Arrays......Page 150
Array Length......Page 152
Looping through Tables......Page 153
The Table Library......Page 157
Object-Oriented Programming with Tables......Page 162
Defining Vararg Functions......Page 165
Scripts as Vararg Functions......Page 169
Keyword Arguments......Page 172
Table Equality......Page 173
Variables and Mutable Values......Page 174
Tables and Functions......Page 176
Copying Tables......Page 177
Building Other Data Structures from Tables......Page 181
Custom-Made Loops......Page 187
Global Variable Environments......Page 192
Summary......Page 197
Exercises......Page 198
Basic String Conversion Functions......Page 200
Converting Between Characters and Character Codes......Page 202
Formatting Strings and Numbers with string.format......Page 203
Input/Output......Page 209
Writing to and Reading from a File......Page 210
Pattern-Matching......Page 214
Matching Any of Several Characters......Page 215
Matches of Varying Lengths......Page 222
Captures......Page 227
More on string.find, string.match, and string.gsub......Page 231
Iterating Through All Matches......Page 233
Tricks for the Tricky......Page 236
Magic Characters Chart......Page 238
Exercises......Page 239
Syntax Errors......Page 242
Runtime Errors......Page 246
Default Error Behavior......Page 247
Checking Assumptions......Page 248
Working with Return Values......Page 251
Structuring Code......Page 253
Error-Containment Functions......Page 256
Summary......Page 259
Exercises......Page 260
Interfaces and Implementations......Page 262
The require Function......Page 263
Creating a Module Directory......Page 264
Preserving a Module’s Interface......Page 265
Module Bookkeeping......Page 269
Bytecode......Page 270
Creating and Reusing Namespaces......Page 271
Avoiding Global Variables......Page 273
The module Function......Page 274
Exercises......Page 276
Using Concatenation and Arithmetical Operators on Tables......Page 278
Relational Metamethods......Page 286
Indexing and Call Metamethods......Page 287
Non-Tables with Metamethods......Page 294
Non-Syntactical Metamethods......Page 296
Summary......Page 297
Exercises......Page 298
Coroutines and Program Control......Page 300
How Coroutines Are Like Programs......Page 301
Coroutines Are Cooperative......Page 302
Coroutines Have Status......Page 307
Rules of Conduct......Page 308
Managing Concurrent Tasks......Page 310
Exercising a Coroutine’s Memory......Page 311
Iterating with Coroutines......Page 315
Handling Events Simply......Page 316
The Event Loop......Page 317
Yielding to Another Coroutine......Page 325
Exercises......Page 326
Bytecode and luac......Page 328
Garbage Collection......Page 332
The Implementation of Tables and Strings......Page 336
Inspecting and Manipulating Running Code......Page 337
Hooks......Page 344
Summary......Page 350
Exercises......Page 351
Core Library......Page 354
Metatable Functions......Page 355
Chunk-Loading Functions......Page 357
Error-Containment Functions......Page 359
Module Functions......Page 360
The Garbage-Collection Function......Page 361
Error-Condition Functions......Page 362
Table Traversal Functions......Page 363
Vararg-Related Functions......Page 364
Coroutine Library......Page 365
Package Library......Page 367
Pattern-Based String Functions......Page 369
String-Conversion Functions......Page 371
Table Library......Page 373
Trigonometric Functions......Page 374
Inverse Trigonometric Functions......Page 377
Hyperbolic Functions......Page 380
Exponent Functions......Page 383
Logarithm Functions......Page 385
Adjustment Functions......Page 387
Floating Point Representation......Page 389
Angle Conversion Functions......Page 390
Modulus Functions......Page 391
Constants......Page 392
Input/Output Library......Page 393
Time and Date Functions......Page 397
Filesystem Functions......Page 398
Debugging Library......Page 399
Summary......Page 402
Library Overview......Page 404
Dynamically Linked Libraries......Page 405
Libraries Built from Source Code......Page 406
The Variable Registration Process......Page 408
Calling a C Function from Lua......Page 409
Building the pack Library on Unix-type Systems......Page 412
Testing the pack Library......Page 413
Using the pack Library......Page 414
Building libcurl......Page 418
Building luacurl......Page 421
Using luacurl......Page 422
Building gd......Page 424
Building lua-gd......Page 426
Using lua-gd......Page 428
Building SQLite3......Page 434
Building lua-sqlite3......Page 436
Using lua-sqlite3......Page 438
Summary......Page 440
Exercises......Page 441
How C Programs Use Lua......Page 442
Embedding or Extending: Which Is Best?......Page 443
Communicating Between Lua and C......Page 444
Calling a Lua Function......Page 450
Protected Calls......Page 451
Working with Userdata......Page 452
Retrieving Indexed Values......Page 465
Setting Indexed Values......Page 466
The Registry......Page 467
Upvalues in C......Page 468
Referencing Values......Page 469
Layering Your Extension Library......Page 470
Summary......Page 476
Exercises......Page 477
Some Basic Relational Database Concepts......Page 478
SQL, LuaSQL, and MySQL......Page 487
Exercises......Page 495
A Web Server Primer......Page 496
Embedded Web Server......Page 497
Executing CGI Scripts......Page 498
CGI Scripts on Windows......Page 499
Apache......Page 500
TinyWeb......Page 501
Serving Dynamic Web Content......Page 503
Problems with CGI Scripts......Page 504
Asynchronous Calls to the Server......Page 505
Producing a Calendar Dynamically......Page 507
Producing Charts Dynamically......Page 510
CGI Helper Routines......Page 518
The Kepler Project......Page 527
CGI the Kepler Way......Page 528
Lua Pages......Page 529
Exercises......Page 530
Installing LuaSocket......Page 532
Compiling LuaSocket......Page 533
Installing Windows Binaries......Page 534
Routed Packets......Page 535
Domain Names......Page 536
Identifying Internet Resources......Page 537
Transport Protocols......Page 538
Sockets: Streams and Datagrams......Page 539
TCP Socket Sociology......Page 540
Using LuaSocket for Network Communication......Page 541
Using Lua Coroutines with the select Function......Page 547
Multiple Connections on the Server Side......Page 551
Setting Timeout Values for the Server Socket......Page 552
Filtering the Flow of Data......Page 553
Accessing Web Pages......Page 556
Sending and Receiving E-mail Messages......Page 558
On the Server Side: inetd and Friends......Page 565
On the Client Side: ssh and Friends......Page 567
Summary......Page 570
Exercises......Page 571
Understanding Why and When to Use Lua......Page 572
Installing SDL and LuaCheia......Page 573
Using SDL......Page 575
Exercise......Page 591
Getting Started with Plua......Page 594
Fitting into the Lua Community......Page 624
The Lua Reference Manual......Page 625
Viewing and Searching the Archives......Page 626
Downloading the Archives......Page 627
Subscribing to the List Server......Page 628
Posting Messages......Page 629
The Lua Wiki......Page 630
LuaForge......Page 631
Summary......Page 632
Examining the Distribution Contents......Page 595
Running the Plua Application......Page 596
Saving Plua Programs......Page 598
Reading the Online Documentation......Page 599
Using Palm OS Streams......Page 600
Compiling Applications......Page 601
Compiling Libraries......Page 602
The Command-Line Compiler......Page 605
The Palm OS Emulator......Page 606
Programming with Plua......Page 610
Generating Graphics......Page 611
Programming the User Interface......Page 612
Accessing Databases......Page 619
Summary......Page 621
Exercises......Page 622
Appendix A: Answers......Page 634
Index......Page 658

Beginning Lua Programming
 9780470069172, 0470069171

  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Recommend Papers