Beginning Lua Programming [NOOK Book]


Lua offers a wide range of features that you can use to support and enhance your applications. With this book as your guide, you’ll gain a thorough understanding of all aspects of programming with this powerful language. The authors present the fundamentals of programming, explain standard Lua functions, and explain how to take advantage of free Lua community resources. Complete code samples are integrated throughout the chapters to clearly demonstrate how to apply the information so that you can quickly write ...

See more details below
Beginning Lua Programming

Available on NOOK devices and apps  
  • NOOK Devices
  • NOOK HD/HD+ Tablet
  • NOOK
  • NOOK Color
  • NOOK Tablet
  • Tablet/Phone
  • NOOK for Windows 8 Tablet
  • NOOK for iOS
  • NOOK for Android
  • NOOK Kids for iPad
  • PC/Mac
  • NOOK for Windows 8
  • NOOK for PC
  • NOOK for Mac
  • NOOK Study
  • NOOK for Web

Want a NOOK? Explore Now

NOOK Book (eBook)
$22.99 price
(Save 42%)$39.99 List Price
Note: This NOOK Book can be purchased in bulk. Please email us for more information.


Lua offers a wide range of features that you can use to support and enhance your applications. With this book as your guide, you’ll gain a thorough understanding of all aspects of programming with this powerful language. The authors present the fundamentals of programming, explain standard Lua functions, and explain how to take advantage of free Lua community resources. Complete code samples are integrated throughout the chapters to clearly demonstrate how to apply the information so that you can quickly write your own programs.

Read More Show Less

Product Details

  • ISBN-13: 9781118079119
  • Publisher: Wiley
  • Publication date: 8/15/2011
  • Sold by: Barnes & Noble
  • Format: eBook
  • Edition number: 1
  • Pages: 672
  • Sales rank: 421,466
  • File size: 3 MB

Meet the Author

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.

Read More Show Less

Table of Contents

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

Customer Reviews

Be the first to write a review
( 0 )
Rating Distribution

5 Star


4 Star


3 Star


2 Star


1 Star


Your Rating:

Your Name: Create a Pen Name or

Barnes & Review Rules

Our reader reviews allow you to share your comments on titles you liked, or didn't, with others. By submitting an online review, you are representing to Barnes & that all information contained in your review is original and accurate in all respects, and that the submission of such content by you and the posting of such content by Barnes & does not and will not violate the rights of any third party. Please follow the rules below to help ensure that your review can be posted.

Reviews by Our Customers Under the Age of 13

We highly value and respect everyone's opinion concerning the titles we offer. However, we cannot allow persons under the age of 13 to have accounts at or to post customer reviews. Please see our Terms of Use for more details.

What to exclude from your review:

Please do not write about reviews, commentary, or information posted on the product page. If you see any errors in the information on the product page, please send us an email.

Reviews should not contain any of the following:

  • - HTML tags, profanity, obscenities, vulgarities, or comments that defame anyone
  • - Time-sensitive information such as tour dates, signings, lectures, etc.
  • - Single-word reviews. Other people will read your review to discover why you liked or didn't like the title. Be descriptive.
  • - Comments focusing on the author or that may ruin the ending for others
  • - Phone numbers, addresses, URLs
  • - Pricing and availability information or alternative ordering information
  • - Advertisements or commercial solicitation


  • - By submitting a review, you grant to Barnes & and its sublicensees the royalty-free, perpetual, irrevocable right and license to use the review in accordance with the Barnes & Terms of Use.
  • - Barnes & reserves the right not to post any review -- particularly those that do not follow the terms and conditions of these Rules. Barnes & also reserves the right to remove any review at any time without notice.
  • - See Terms of Use for other conditions and disclaimers.
Search for Products You'd Like to Recommend

Recommend other products that relate to your review. Just search for them below and share!

Create a Pen Name

Your Pen Name is your unique identity on It will appear on the reviews you write and other website activities. Your Pen Name cannot be edited, changed or deleted once submitted.

Your Pen Name can be any combination of alphanumeric characters (plus - and _), and must be at least two characters long.

Continue Anonymously

    If you find inappropriate content, please report it to Barnes & Noble
    Why is this product inappropriate?
    Comments (optional)