
Beginning Lua Programming
by Jung, Kurt; Brown, AaronRent Book
New Book
We're Sorry
Sold Out
Used Book
We're Sorry
Sold Out
eBook
We're Sorry
Not Available
How Marketplace Works:
- This item is offered by an independent seller and not shipped from our warehouse
- Item details like edition and cover design may differ from our description; see seller's comments before ordering.
- Sellers much confirm and ship within two business days; otherwise, the order will be cancelled and refunded.
- Marketplace purchases cannot be returned to eCampus.com. Contact the seller directly for inquiries; if no response within two days, contact customer service.
- Additional shipping costs apply to Marketplace purchases. Review shipping costs at checkout.
Summary
Author Biography
Aaron Brown began programming in elementary school on a Commodore 64. He plays various musical instruments and speaks Esperanto.
Table of Contents
Acknowledgments | p. ix |
Introduction | p. xxiii |
Getting Situated | p. 1 |
Choosing How to Install Lua | p. 1 |
Building Lua Yourself | p. 2 |
Selecting Prebuilt Lua | p. 3 |
Finding Your System's Shell | p. 3 |
Windows Shells | p. 3 |
Shells on Unix and Unix-Like systems | p. 3 |
Shell Features | p. 4 |
The Environment | p. 4 |
Environment Variables on Unix-Like Systems | p. 4 |
Environment Variables on Windows | p. 5 |
Dealing with Tarballs and Zip Files | p. 6 |
Compiling Lua | p. 7 |
The Lua Source Tarball | p. 7 |
Compiling Lua on Linux and Other Unix-Like Systems | p. 8 |
Compiling Lua on Windows | p. 12 |
Building Lua with Microsoft Visual C++ | p. 13 |
Building Lua with the Tiny C Compiler | p. 14 |
Building Lua with MinGW | p. 16 |
Binary Packages | p. 13 |
Selecting a Prebuilt Binary Package | p. 18 |
Installing a Prebuilt Binary Package on a Unix-Type System | p. 19 |
Installing a Prebuilt Binary Package on Windows | p. 20 |
Additional Tools | p. 21 |
Programmer's Editor | p. 21 |
Revision Control System | p. 22 |
Summary | p. 22 |
First Steps | p. 23 |
Numbers and Arithmetic Operations: Basic Interpreter Usage | p. 23 |
Addition, Subtraction, Multiplication, Division, and Exponentiation | p. 24 |
Interacting with the Interpreter | p. 24 |
Other Notations for Numbers | p. 25 |
Interpreter Know-How | p. 26 |
Quitting the Interpreter | p. 26 |
Interpreter Shortcuts | p. 26 |
Numerical Gotchas | p. 27 |
Division by Zero and Overflow | p. 27 |
Floating-Point Rounding | p. 28 |
Variables and Assignment | p. 28 |
Assignment Basics | p. 29 |
Multiple Assignment | p. 31 |
Variables on the Right Side of Assignments | p. 32 |
Strings | p. 32 |
Quoting Strings | p. 32 |
Quoting Strings with Double Quotes | p. 32 |
Quoting Strings with Single Quotes | p. 33 |
Quoting Strings with Square Brackets | p. 33 |
Backslash Escaping | p. 35 |
Relational Operators and Boolean Values | p. 37 |
Comparing Numbers | p. 37 |
Comparing Strings | p. 38 |
The nil Value | p. 40 |
Boolean Operators | p. 41 |
The and Operator | p. 42 |
The or Operator | p. 43 |
The not Unary Operator | p. 44 |
The Concatenation, Length, and Modulo Operators | p. 45 |
The String Concatenation Operator | p. 45 |
The Length Operator | p. 46 |
The Modulo Operator | p. 47 |
Automatic Conversion of Operands | p. 48 |
Precedence and Associativity | p. 49 |
Variables and Values | p. 51 |
Comments | p. 52 |
Expressions and Statements | p. 53 |
Compound Statements | p. 54 |
The if Statement | p. 55 |
The while Loop | p. 58 |
The for Loop | p. 60 |
The repeat Loop | p. 62 |
The break and do Statements | p. 63 |
Summary | p. 66 |
Exercises | p. 66 |
Extending Lua with Functions | p. 69 |
Return Values | p. 72 |
Using a Function that Returns a Value | p. 72 |
Defining a Function that Returns a Value | p. 73 |
Using return to Alter Control Flow | p. 74 |
Returning Nothing | p. 76 |
Returning Multiple Values | p. 77 |
Adjusting Value Lists | p. 78 |
Using Multiple-Valued Functions in Value Lists | p. 78 |
Using Valueless Functions in Value Lists | p. 79 |
Chunks as Functions | p. 81 |
Variable Scope | p. 84 |
Actual and Formal Arguments | p. 84 |
Local Variables | p. 85 |
Understanding Side Effects | p. 91 |
Ordering Side Effects | p. 91 |
Short-Circuit Evaluation | p. 93 |
Functions Calling Functions | p. 95 |
The Call Stack | p. 95 |
Recursion | p. 97 |
Stack Overflow | p. 98 |
Tail Calls | p. 99 |
Functions as Values | p. 102 |
Replacing Built-In Functions | p. 102 |
Comparing and Printing Functions | p. 103 |
Function Definitions as Assignments | p. 103 |
Local Functions | p. 105 |
Whitespace, Semicolons, and Function Calls | p. 106 |
Upvalues and Closures | p. 108 |
Defining Functions that Create Functions | p. 108 |
Defining Functions with Private State | p. 110 |
Figuring Out Tricky Scope Situations | p. 111 |
Summary | p. 113 |
Exercises | p. 114 |
Working with Tables | p. 117 |
Tables Introduced | p. 117 |
A Shorter Way to Write Some Keys | p. 119 |
Altering a Table's Contents | p. 120 |
Tables as Arrays | p. 121 |
Array Length | p. 123 |
Looping through Tables | p. 124 |
Tables of Functions | p. 128 |
The Table Library | p. 128 |
table.sort | p. 128 |
table.concat | p. 131 |
table.remove | p. 132 |
table.maxn | p. 132 |
Object-Oriented Programming with Tables | p. 133 |
Functions with Variable Numbers of Arguments | p. 136 |
Defining Vararg Functions | p. 136 |
Scripts as Vararg Functions | p. 140 |
Keyword Arguments | p. 143 |
Different but the Same | p. 144 |
Table Equality | p. 144 |
Avoiding Bugs by Understanding Mutability | p. 145 |
Variables and Mutable Values | p. 145 |
Tables and Functions | p. 147 |
Copying Tables | p. 148 |
Building Other Data Structures from Tables | p. 152 |
Custom-Made Loops | p. 158 |
Global Variable Environments | p. 163 |
Summary | p. 168 |
Exercises | p. 169 |
Using Strings | p. 171 |
Basic String Conversion Functions | p. 171 |
String Length | p. 173 |
Converting Between Characters and Character Codes | p. 173 |
Formatting Strings and Numbers with string.format | p. 174 |
Input/Output | p. 180 |
Writing to and Reading from a File | p. 181 |
Pattern-Matching | p. 185 |
Searching for a Specific String | p. 186 |
Matching Any of Several Characters | p. 186 |
Matches of Varying Lengths | p. 193 |
Captures | p. 198 |
Matching Balanced Delimiters | p. 202 |
More on string.find, string.match, and string.gsub | p. 202 |
Iterating Through All Matches | p. 204 |
Tricks for the Tricky | p. 207 |
Magic Characters Chart | p. 209 |
Summary | p. 210 |
Exercises | p. 210 |
Handling and Avoiding Errors | p. 213 |
Kinds of Errors | p. 213 |
Syntax Errors | p. 213 |
Runtime Errors | p. 217 |
Handling Errors | p. 218 |
Default Error Behavior | p. 218 |
Checking Assumptions | p. 219 |
Code Errors | p. 220 |
Data Errors | p. 220 |
The assert and error Functions | p. 220 |
Defining Your Own Error Condition | p. 221 |
Anticipating Error Conditions | p. 222 |
Working with Return Values | p. 222 |
Structuring Code | p. 224 |
Error-Containment Functions | p. 227 |
The pcall Function | p. 227 |
The xpcall Function | p. 229 |
User-Written Scripts | p. 230 |
Locating Errors | p. 230 |
Summary | p. 230 |
Exercises | p. 231 |
Using Modules | p. 233 |
Interfaces and Implementations | p. 233 |
The require Function | p. 234 |
Where to Put Modules | p. 235 |
Creating a Module Directory | p. 235 |
Setting Lua's Environment Variable | p. 236 |
Preserving a Module's Interface | p. 236 |
Module Bookkeeping | p. 240 |
Bytecode | p. 241 |
Namespaces | p. 242 |
Creating and Reusing Namespaces | p. 242 |
Avoiding Global Variables | p. 244 |
Using the strict Module | p. 244 |
Reporting All Global Assignments | p. 244 |
The module Fynction | p. 245 |
C Modules | p. 247 |
Summary | p. 247 |
Exercises | p. 247 |
Extending Lua's Behavior with Metamethods | p. 249 |
Using Concatenation and Arithmetical Operators on Tables | p. 249 |
Relational Metamethods | p. 257 |
Indexing and Call Metamethods | p. 258 |
Non-Tables with Metamethods | p. 265 |
Non-Syntactical Metamethods | p. 267 |
Metamethod Applicability | p. 268 |
Summary | p. 268 |
Exercises | p. 269 |
Handling Events Naturally with Coroutines | p. 271 |
Coroutines and Program Control | p. 271 |
Coroutines Are Not Functions | p. 272 |
How Coroutines Are Like Programs | p. 272 |
Coroutines Transfer Control | p. 273 |
Wrapping a Coroutine | p. 273 |
Coroutines Are Cooperative | p. 273 |
Outside Looking In | p. 275 |
Coroutines Have Status | p. 278 |
Rules of Conduct | p. 279 |
Work Shoulder-to-Shoulder | p. 279 |
Trust the Dispatcher | p. 280 |
Expect the Best, Prepare for the Worst | p. 280 |
Play on Your Side of the Fence | p. 280 |
Avoid the Deep End | p. 281 |
Managing Concurrent Tasks | p. 281 |
Retaining State | p. 282 |
Exercising a Coroutine's Memory | p. 282 |
Iterating with Coroutines | p. 286 |
Handling Events Simply | p. 287 |
The Event Loop | p. 288 |
Yielding to Another Coroutine | p. 296 |
Summary | p. 297 |
Exercises | p. 297 |
Looking Under the Hood | p. 299 |
Bytecode and luac | p. 299 |
Garbage Collection | p. 303 |
The Implementation of Tables and Strings | p. 307 |
The Debug Library | p. 308 |
Inspecting and Manipulating Running Code | p. 308 |
Hooks | p. 315 |
Other Functions in the Debug Library | p. 321 |
Summary | p. 321 |
Exercises | p. 322 |
Exploring Lua's Libraries | p. 325 |
Core Library | p. 325 |
Environment Functions | p. 325 |
Metatable Functions | p. 326 |
Chunk-Loading Functions | p. 328 |
Error-Containment Functions | p. 330 |
Module Functions | p. 331 |
The Garbage-Collection Function | p. 332 |
Type and Conversion Functions | p. 333 |
Basic Output | p. 333 |
Error-Condition Functions | p. 333 |
Table Traversal Functions | p. 334 |
Vararg-Related Functions | p. 335 |
Coroutine Library | p. 336 |
Package Library | p. 338 |
String Library | p. 340 |
Pattern-Based String Functions | p. 340 |
String-Conversion Functions | p. 342 |
Table Library | p. 344 |
Math Library | p. 345 |
Trigonometric Functions | p. 345 |
Inverse Trigonometric Functions | p. 348 |
Hyperbolic Functions | p. 351 |
Exponent Functions | p. 354 |
Logarithm Functions | p. 356 |
Adjustment Functions | p. 358 |
Floating Point Representation | p. 360 |
Angle Conversion Functions | p. 361 |
Pseudo-Random Number Functions | p. 362 |
Modulus Functions | p. 362 |
Minimum and Maximum Functions | p. 363 |
Constants | p. 363 |
Input/Output Library | p. 364 |
Operating System Library | p. 368 |
CPU Timing | p. 368 |
Time and Date Functions | p. 368 |
Filesystem Functions | p. 369 |
Other Operating System Functions | p. 370 |
Debugging Library | p. 370 |
Summary | p. 373 |
Using Community Libraries | p. 375 |
Library Overview | p. 375 |
Dynamically Linked Libraries | p. 376 |
Resolving External References | p. 376 |
Configuration Options | p. 376 |
Libraries Built from Source Code | p. 377 |
Building Libraries on Unix-Like Systems | p. 378 |
Building Libraries on Windows | p. 378 |
Limits to Portability | p. 379 |
How Lua Interacts with Libraries | p. 379 |
The Variable Registration Process | p. 379 |
Calling a C Function from Lua | p. 380 |
The pack Binary Structuring Library | p. 383 |
Building the pack Library on Unix-type Systems | p. 383 |
Building and Installing the pack Library on Windows | p. 384 |
Testing the pack Library | p. 384 |
Installing the pack Library | p. 385 |
Using the pack Library | p. 385 |
The cURL File Transfer Library | p. 389 |
Building libcurl | p. 389 |
Building libcurl on Unix-Like Systems | p. 390 |
Building libcurl on Windows | p. 391 |
Building luacurl | p. 392 |
Building luacurl on Unix-Like Systems | p. 392 |
Building luacurl on Windows | p. 393 |
Using luacurl | p. 393 |
The gd Graphics Library | p. 395 |
Building gd | p. 395 |
Building gd on Unix-Like Systems | p. 396 |
Installing gd on Windows | p. 396 |
Building lua-gd | p. 397 |
Building lua-gd on Unix-Like Systems | p. 397 |
Building lua-gd on Windows | p. 398 |
Using lua-gd | p. 399 |
The SQLite Database Library | p. 405 |
Building SQLite3 | p. 405 |
Building SQLite3 on Unix-Like Systems | p. 405 |
Building SQLite3 on Windows | p. 406 |
Building lua-sqlite3 | p. 407 |
Building lua-sqlite3 on Unix-Like Systems | p. 407 |
Building lua-sqlite3 on Windows | p. 408 |
Using lua-sqlite3 | p. 409 |
Summary | p. 411 |
Exercises | p. 412 |
Interfacing Lua with Other Languages | p. 413 |
How C Programs Use Lua | p. 413 |
Embedding Lua | p. 414 |
Extending Lua | p. 414 |
Embedding or Extending: Which Is Best? | p. 414 |
Communicating Between Lua and C | p. 415 |
Calling Lua from C | p. 421 |
Obtaining a Lua Function | p. 421 |
Calling a Lua Function | p. 421 |
Protected Calls | p. 422 |
Working with Userdata | p. 423 |
Indexing Values in C | p. 436 |
Retrieving Indexed Values | p. 436 |
Setting Indexed Values | p. 437 |
Retaining Values in C | p. 438 |
The Registry | p. 438 |
C Function Environments | p. 439 |
Upvalues in C | p. 439 |
Referencing Values | p. 440 |
The Thread Environment | p. 441 |
Layering Your Extension Library | p. 441 |
Summary | p. 447 |
Exercises | p. 448 |
Managing Information with Databases | p. 449 |
Some Basic Relational Database Concepts | p. 449 |
SQL, LuaSQL, and MySQL | p. 458 |
Summary | p. 466 |
Exercises | p. 466 |
Programming for the Web | p. 467 |
A Web Server Primer | p. 467 |
Dynamic Web Content | p. 468 |
Embedded Web Server | p. 468 |
Extended Web Server | p. 469 |
Creating Content at Run Time with Lua | p. 469 |
Executing CGI Scripts | p. 469 |
CGI Scripts on Unix-Type Systems | p. 470 |
CGI Scripts on Windows | p. 470 |
Installing a Web Server | p. 471 |
Apache | p. 471 |
TinyWeb | p. 472 |
Testing Your Web Server with Static Content | p. 474 |
Serving Dynamic Web Content | p. 474 |
Problems with CGI Scripts | p. 475 |
Asynchronous Calls to the Server | p. 476 |
Producing a Calendar Dynamically | p. 478 |
Producing Charts Dynamically | p. 481 |
Interactive CGI Applications | p. 489 |
CGI Helper Routines | p. 489 |
Developing CGI Scripts | p. 498 |
Security Issues | p. 498 |
The Kepler Project | p. 498 |
CGI the Kepler Way | p. 499 |
Lua Pages | p. 500 |
Summary | p. 501 |
Exercises | p. 501 |
Connecting to a Larger World | p. 503 |
Installing LuaSocket | p. 503 |
Compiling LuaSocket | p. 504 |
Compiling on Linux and Other Unix-Like Systems | p. 504 |
Compiling on Windows | p. 504 |
Installing Windows Binaries | p. 505 |
Network Overview | p. 506 |
Routed Packets | p. 506 |
Addresses | p. 507 |
Domain Names | p. 507 |
Identifying Internet Resources | p. 508 |
Transport Protocols | p. 509 |
Sockets: Streams and Datagrams | p. 510 |
TCP Socket Sociology | p. 511 |
Using LuaSocket for Network Communication | p. 512 |
Handling Multiple Persistent Connections | p. 518 |
Using Lua Coroutines with the select Function | p. 518 |
Multiple Connections on the Server Side | p. 522 |
Setting Timeout Values for the Server Socket | p. 523 |
The Application Protocols | p. 524 |
Filtering the Flow of Data | p. 524 |
Accessing Web Pages | p. 527 |
Sending and Receiving E-mail Messages | p. 529 |
Networking with Lua and Streams | p. 536 |
On the Server Side: inetd and Friends | p. 536 |
On the Client Side: ssh and Friends | p. 538 |
Summary | p. 541 |
Exercises | p. 542 |
Programming Games with Lua | p. 543 |
Understanding Why and When to Use Lua | p. 543 |
Simple 2-D Action Game Using SDL | p. 544 |
Installing SDL and LuaCheia | p. 544 |
Using SDL | p. 546 |
Summary | p. 562 |
Exercise | p. 562 |
Carrying Lua with You | p. 565 |
Getting Started with Plua | p. 565 |
Obtaining Plua | p. 566 |
Examining the Distribution Contents | p. 566 |
Exploring Plua's Features | p. 567 |
Running the Plua Application | p. 567 |
Saving Plua Programs | p. 569 |
Reading the Online Documentation | p. 570 |
Using Palm OS Streams | p. 571 |
Compiling Applications | p. 572 |
Compiling Libraries | p. 573 |
Plua on the Mothership | p. 576 |
The Command-Line Compiler | p. 576 |
The Palm OS Emulator | p. 577 |
Obtaining the Emulator | p. 577 |
Installing on Windows | p. 578 |
Configuring POSE | p. 578 |
Running Plua in the Emulator | p. 578 |
Compiling a Program in the Emulator | p. 580 |
Exiting the Emulator | p. 580 |
The Palm OS Simulator | p. 581 |
Obtaining the Simulator | p. 581 |
Using the Simulator | p. 581 |
Programming with Plua | p. 581 |
Generating Graphics | p. 582 |
Programming the User Interface | p. 583 |
Accessing Databases | p. 590 |
Summary | p. 592 |
Exercises | p. 593 |
Fitting into the Lua Community | p. 595 |
The Lua Web Site | p. 596 |
The Lua Reference Manual | p. 596 |
Framing Questions | p. 597 |
The Lua Mailing List | p. 597 |
Viewing and Searching the Archives | p. 597 |
Downloading the Archives | p. 598 |
Using a Web Browser to Access the List | p. 599 |
Using a Newsreader to Access the List | p. 599 |
Subscribing to the List Server | p. 599 |
Posting Messages | p. 600 |
The Lua Chat Room | p. 601 |
Forums | p. 601 |
The Lua Wiki | p. 601 |
LuaForge | p. 602 |
Annual Workshops | p. 603 |
Summary | p. 603 |
Answers | p. 605 |
Index | p. 629 |
Table of Contents provided by Ingram. All Rights Reserved. |
An electronic version of this book is available through VitalSource.
This book is viewable on PC, Mac, iPhone, iPad, iPod Touch, and most smartphones.
By purchasing, you will be able to view this book online, as well as download it, for the chosen number of days.
Digital License
You are licensing a digital product for a set duration. Durations are set forth in the product description, with "Lifetime" typically meaning five (5) years of online access and permanent download to a supported device. All licenses are non-transferable.
More details can be found here.
A downloadable version of this book is available through the eCampus Reader or compatible Adobe readers.
Applications are available on iOS, Android, PC, Mac, and Windows Mobile platforms.
Please view the compatibility matrix prior to purchase.