You are previewing Beginning Lua Programming.

Beginning Lua Programming

Cover of Beginning Lua Programming by Aaron Brown... Published by Wrox
  1. Copyright
  2. About the Authors
  3. Credits
  4. Acknowledgments
  5. Introduction
    1. 0.1. The Facets of Lua
      1. 0.1.1. Lua Is a Programming Language
      2. 0.1.2. Lua Is an Implementation
      3. 0.1.3. Lua Is Fast
      4. 0.1.4. Lua Is Free and Open
    2. 0.2. Who This Book Is For
    3. 0.3. How This Book Is Structured
    4. 0.4. What You Need to Use This Book
    5. 0.5. Conventions
      1. 0.5.1.
    6. 0.6. Source Code
    7. 0.7. Errata
    8. 0.8. p2p.wrox.com
  6. 1. Getting Situated
    1. 1.1. Choosing How to Install Lua
      1. 1.1.1. Building Lua Yourself
      2. 1.1.2. Selecting Prebuilt Lua
    2. 1.2. Finding Your System's Shell
      1. 1.2.1. Windows Shells
      2. 1.2.2. Shells on Unix and Unix-Like systems
      3. 1.2.3. Shell Features
      4. 1.2.4. The Environment
    3. 1.3. Dealing with Tarballs and Zip Files
    4. 1.4. Compiling Lua
      1. 1.4.1. The Lua Source Tarball
      2. 1.4.2. Compiling Lua on Linux and Other Unix-Like Systems
      3. 1.4.3. Compiling Lua on Windows
    5. 1.5. Binary Packages
      1. 1.5.1. Selecting a Prebuilt Binary Package
      2. 1.5.2. Installing a Prebuilt Binary Package on a Unix-Type System
      3. 1.5.3. Installing a Prebuilt Binary Package on Windows
    6. 1.6. Additional Tools
      1. 1.6.1. Programmer's Editor
      2. 1.6.2. Revision Control System
    7. 1.7. Summary
  7. 2. First Steps
    1. 2.1. Numbers and Arithmetic Operations: Basic Interpreter Usage
      1. 2.1.1. Addition, Subtraction, Multiplication, Division, and Exponentiation
      2. 2.1.2. Interacting with the Interpreter
      3. 2.1.3. Other Notations for Numbers
    2. 2.2. Interpreter Know-How
      1. 2.2.1. Quitting the Interpreter
      2. 2.2.2. Interpreter Shortcuts
    3. 2.3. Numerical Gotchas
      1. 2.3.1. Division by Zero and Overflow
      2. 2.3.2. Floating-Point Rounding
    4. 2.4. Variables and Assignment
      1. 2.4.1. Assignment Basics
      2. 2.4.2. Multiple Assignment
      3. 2.4.3. Variables on the Right Side of Assignments
    5. 2.5. Strings
      1. 2.5.1. Quoting Strings
      2. 2.5.2. Backslash Escaping
    6. 2.6. Relational Operators and Boolean Values
      1. 2.6.1. Comparing Numbers
      2. 2.6.2. Comparing Strings
    7. 2.7. The nil Value
    8. 2.8. Boolean Operators
      1. 2.8.1. The and Operator
      2. 2.8.2. The or Operator
      3. 2.8.3. The not Unary Operator
    9. 2.9. The Concatenation, Length, and Modulo Operators
      1. 2.9.1. The String Concatenation Operator
      2. 2.9.2. The Length Operator
      3. 2.9.3. The Modulo Operator
    10. 2.10. Automatic Conversion of Operands
    11. 2.11. Precedence and Associativity
    12. 2.12. Variables and Values
    13. 2.13. Comments
    14. 2.14. Expressions and Statements
    15. 2.15. Compound Statements
      1. 2.15.1. The if Statement
      2. 2.15.2. The while Loop
      3. 2.15.3. The for Loop
      4. 2.15.4. The repeat Loop
      5. 2.15.5. The break and do Statements
    16. 2.16. Summary
    17. 2.17. Exercises
  8. 3. Extending Lua with Functions
    1. 3.1. Return Values
      1. 3.1.1. Using a Function that Returns a Value
      2. 3.1.2. Defining a Function that Returns a Value
      3. 3.1.3. Using return to Alter Control Flow
      4. 3.1.4. Returning Nothing
      5. 3.1.5. Returning Multiple Values
      6. 3.1.6. Adjusting Value Lists
    2. 3.2. Chunks as Functions
    3. 3.3. Variable Scope
      1. 3.3.1. Actual and Formal Arguments
      2. 3.3.2. Local Variables
    4. 3.4. Understanding Side Effects
      1. 3.4.1. Ordering Side Effects
      2. 3.4.2. Short-Circuit Evaluation
    5. 3.5. Functions Calling Functions
      1. 3.5.1. The Call Stack
      2. 3.5.2. Recursion
      3. 3.5.3. Stack Overflow
      4. 3.5.4. Tail Calls
    6. 3.6. Functions as Values
      1. 3.6.1. Replacing Built-In Functions
      2. 3.6.2. Comparing and Printing Functions
      3. 3.6.3. Function Definitions as Assignments
      4. 3.6.4. Local Functions
    7. 3.7. Whitespace, Semicolons, and Function Calls
    8. 3.8. Upvalues and Closures
      1. 3.8.1. Defining Functions that Create Functions
      2. 3.8.2. Defining Functions with Private State
      3. 3.8.3. Figuring Out Tricky Scope Situations
    9. 3.9. Summary
    10. 3.10. Exercises
  9. 4. Working with Tables
    1. 4.1. Tables Introduced
    2. 4.2. A Shorter Way to Write Some Keys
    3. 4.3. Altering a Table's Contents
    4. 4.4. Tables as Arrays
    5. 4.5. Array Length
    6. 4.6. Looping through Tables
    7. 4.7. Tables of Functions
      1. 4.7.1. The Table Library
      2. 4.7.2. Object-Oriented Programming with Tables
    8. 4.8. Functions with Variable Numbers of Arguments
      1. 4.8.1. Defining Vararg Functions
      2. 4.8.2. Scripts as Vararg Functions
    9. 4.9. Keyword Arguments
    10. 4.10. Different but the Same
      1. 4.10.1. Table Equality
      2. 4.10.2. Avoiding Bugs by Understanding Mutability
      3. 4.10.3. Variables and Mutable Values
      4. 4.10.4. Tables and Functions
      5. 4.10.5. Copying Tables
    11. 4.11. Building Other Data Structures from Tables
    12. 4.12. Custom-Made Loops
    13. 4.13. Global Variable Environments
    14. 4.14. Summary
    15. 4.15. Exercises
  10. 5. Using Strings
    1. 5.1. Basic String Conversion Functions
    2. 5.2. String Length
    3. 5.3. Converting Between Characters and Character Codes
    4. 5.4. Formatting Strings and Numbers with string.format
    5. 5.5. Input/Output
      1. 5.5.1. Writing to and Reading from a File
    6. 5.6. Pattern-Matching
      1. 5.6.1. Searching for a Specific String
      2. 5.6.2. Matching Any of Several Characters
      3. 5.6.3. Matches of Varying Lengths
      4. 5.6.4. Captures
      5. 5.6.5. Matching Balanced Delimiters
      6. 5.6.6. More on string.find, string.match, and string.gsub
      7. 5.6.7. Iterating Through All Matches
      8. 5.6.8. Tricks for the Tricky
      9. 5.6.9. Magic Characters Chart
    7. 5.7. Summary
    8. 5.8. Exercises
  11. 6. Handling and Avoiding Errors
    1. 6.1. Kinds of Errors
      1. 6.1.1. Syntax Errors
      2. 6.1.2. Runtime Errors
    2. 6.2. Handling Errors
      1. 6.2.1. Default Error Behavior
      2. 6.2.2. Checking Assumptions
      3. 6.2.3. Anticipating Error Conditions
      4. 6.2.4. Working with Return Values
      5. 6.2.5. Structuring Code
      6. 6.2.6. Error-Containment Functions
    3. 6.3. Locating Errors
    4. 6.4. Summary
    5. 6.5. Exercises
  12. 7. Using Modules
    1. 7.1. Interfaces and Implementations
    2. 7.2. The require Function
    3. 7.3. Where to Put Modules
      1. 7.3.1. Creating a Module Directory
      2. 7.3.2. Setting Lua's Environment Variable
    4. 7.4. Preserving a Module's Interface
      1. 7.4.1.
    5. 7.5. Module Bookkeeping
    6. 7.6. Bytecode
    7. 7.7. Namespaces
      1. 7.7.1. Creating and Reusing Namespaces
      2. 7.7.2. Avoiding Global Variables
    8. 7.8. The module Function
    9. 7.9. C Modules
    10. 7.10. Summary
    11. 7.11. Exercises
  13. 8. Extending Lua's Behavior with Metamethods
    1. 8.1. Using Concatenation and Arithmetical Operators on Tables
    2. 8.2. Relational Metamethods
    3. 8.3. Indexing and Call Metamethods
    4. 8.4. Non-Tables with Metamethods
    5. 8.5. Non-Syntactical Metamethods
    6. 8.6. Metamethod Applicability
    7. 8.7. Summary
    8. 8.8. Exercises
  14. 9. Handling Events Naturally with Coroutines
    1. 9.1. Coroutines and Program Control
      1. 9.1.1. Coroutines Are Not Functions
      2. 9.1.2. How Coroutines Are Like Programs
      3. 9.1.3. Coroutines Are Cooperative
      4. 9.1.4. Coroutines Have Status
      5. 9.1.5. Rules of Conduct
    2. 9.2. Managing Concurrent Tasks
      1. 9.2.1.
    3. 9.3. Retaining State
      1. 9.3.1. Exercising a Coroutine's Memory
      2. 9.3.2. Iterating with Coroutines
    4. 9.4. Handling Events Simply
      1. 9.4.1. The Event Loop
      2. 9.4.2. Yielding to Another Coroutine
    5. 9.5. Summary
    6. 9.6. Exercises
  15. 10. Looking Under the Hood
    1. 10.1. Bytecode and luac
    2. 10.2. Garbage Collection
    3. 10.3. The Implementation of Tables and Strings
    4. 10.4. The Debug Library
      1. 10.4.1. Inspecting and Manipulating Running Code
      2. 10.4.2. Hooks
      3. 10.4.3. Other Functions in the Debug Library
    5. 10.5. Summary
    6. 10.6. Exercises
  16. 11. Exploring Lua's Libraries
    1. 11.1. Core Library
      1. 11.1.1. Environment Functions
      2. 11.1.2. Metatable Functions
      3. 11.1.3. Chunk-Loading Functions
      4. 11.1.4. Error-Containment Functions
      5. 11.1.5. Module Functions
      6. 11.1.6. The Garbage-Collection Function
      7. 11.1.7. Type and Conversion Functions
      8. 11.1.8. Basic Output
      9. 11.1.9. Error-Condition Functions
      10. 11.1.10. Table Traversal Functions
      11. 11.1.11. Vararg-Related Functions
    2. 11.2. Coroutine Library
    3. 11.3. Package Library
    4. 11.4. String Library
      1. 11.4.1. Pattern-Based String Functions
      2. 11.4.2. String-Conversion Functions
    5. 11.5. Table Library
    6. 11.6. Math Library
      1. 11.6.1. Trigonometric Functions
      2. 11.6.2. Inverse Trigonometric Functions
      3. 11.6.3. Hyperbolic Functions
      4. 11.6.4. Exponent Functions
      5. 11.6.5. Logarithm Functions
      6. 11.6.6. Adjustment Functions
      7. 11.6.7. Floating Point Representation
      8. 11.6.8. Angle Conversion Functions
      9. 11.6.9. Pseudo-Random Number Functions
      10. 11.6.10. Modulus Functions
      11. 11.6.11. Minimum and Maximum Functions
      12. 11.6.12. Constants
    7. 11.7. Input/Output Library
    8. 11.8. Operating System Library
      1. 11.8.1. CPU Timing
      2. 11.8.2. Time and Date Functions
      3. 11.8.3. Filesystem Functions
      4. 11.8.4. Other Operating System Functions
    9. 11.9. Debugging Library
    10. 11.10. Summary
  17. 12. Using Community Libraries
    1. 12.1. Library Overview
      1. 12.1.1. Dynamically Linked Libraries
      2. 12.1.2. Libraries Built from Source Code
      3. 12.1.3. Limits to Portability
    2. 12.2. How Lua Interacts with Libraries
      1. 12.2.1. The Variable Registration Process
      2. 12.2.2. Calling a C Function from Lua
    3. 12.3. The pack Binary Structuring Library
      1. 12.3.1. Building the pack Library on Unix-type Systems
      2. 12.3.2. Building and Installing the pack Library on Windows
      3. 12.3.3. Testing the pack Library
      4. 12.3.4. Installing the pack Library
      5. 12.3.5. Using the pack Library
    4. 12.4. The cURL File Transfer Library
      1. 12.4.1. Building libcurl
      2. 12.4.2. Building luacurl
      3. 12.4.3. Using luacurl
    5. 12.5. The gd Graphics Library
      1. 12.5.1. Building gd
      2. 12.5.2. Building lua-gd
      3. 12.5.3. Using lua-gd
    6. 12.6. The SQLite Database Library
      1. 12.6.1. Building SQLite3
      2. 12.6.2. Building lua-sqlite3
      3. 12.6.3. Using lua-sqlite3
    7. 12.7. Summary
    8. 12.8. Exercises
  18. 13. Interfacing Lua with Other Languages
    1. 13.1. How C Programs Use Lua
      1. 13.1.1. Embedding Lua
      2. 13.1.2. Extending Lua
      3. 13.1.3. Embedding or Extending: Which Is Best?
    2. 13.2. Communicating Between Lua and C
      1. 13.2.1.
    3. 13.3. Calling Lua from C
      1. 13.3.1. Obtaining a Lua Function
      2. 13.3.2. Calling a Lua Function
      3. 13.3.3. Protected Calls
    4. 13.4. Working with Userdata
      1. 13.4.1.
    5. 13.5. Indexing Values in C
      1. 13.5.1. Retrieving Indexed Values
      2. 13.5.2. Setting Indexed Values
    6. 13.6. Retaining Values in C
      1. 13.6.1. The Registry
      2. 13.6.2. C Function Environments
      3. 13.6.3. Upvalues in C
      4. 13.6.4. Referencing Values
      5. 13.6.5. The Thread Environment
    7. 13.7. Layering Your Extension Library
      1. 13.7.1.
    8. 13.8. Summary
    9. 13.9. Exercises
  19. 14. Managing Information with Databases
    1. 14.1. Some Basic Relational Database Concepts
    2. 14.2. SQL, LuaSQL, and MySQL
      1. 14.2.1.
    3. 14.3. Summary
    4. 14.4. Exercises
  20. 15. Programming for the Web
    1. 15.1. A Web Server Primer
    2. 15.2. Dynamic Web Content
      1. 15.2.1. Embedded Web Server
      2. 15.2.2. Extended Web Server
      3. 15.2.3. Creating Content at Run Time with Lua
    3. 15.3. Executing CGI Scripts
      1. 15.3.1. CGI Scripts on Unix-Type Systems
      2. 15.3.2. CGI Scripts on Windows
    4. 15.4. Installing a Web Server
      1. 15.4.1. Apache
      2. 15.4.2. TinyWeb
    5. 15.5. Testing Your Web Server with Static Content
    6. 15.6. Serving Dynamic Web Content
      1. 15.6.1.
      2. 15.6.2. Problems with CGI Scripts
      3. 15.6.3. Asynchronous Calls to the Server
      4. 15.6.4. Producing a Calendar Dynamically
      5. 15.6.5. Producing Charts Dynamically
    7. 15.7. Interactive CGI Applications
      1. 15.7.1. CGI Helper Routines
      2. 15.7.2. Developing CGI Scripts
      3. 15.7.3. Security Issues
    8. 15.8. The Kepler Project
      1. 15.8.1. CGI the Kepler Way
      2. 15.8.2. Lua Pages
    9. 15.9. Summary
    10. 15.10. Exercises
  21. 16. Connecting to a Larger World
    1. 16.1. Installing LuaSocket
      1. 16.1.1. Compiling LuaSocket
      2. 16.1.2. Installing Windows Binaries
    2. 16.2. Network Overview
      1. 16.2.1. Routed Packets
      2. 16.2.2. Addresses
      3. 16.2.3. Domain Names
      4. 16.2.4. Identifying Internet Resources
      5. 16.2.5. Transport Protocols
      6. 16.2.6. Sockets: Streams and Datagrams
      7. 16.2.7. TCP Socket Sociology
    3. 16.3. Using LuaSocket for Network Communication
      1. 16.3.1.
    4. 16.4. Handling Multiple Persistent Connections
      1. 16.4.1. Using Lua Coroutines with the select Function
      2. 16.4.2. Multiple Connections on the Server Side
      3. 16.4.3. Setting Timeout Values for the Server Socket
    5. 16.5. The Application Protocols
      1. 16.5.1. Filtering the Flow of Data
      2. 16.5.2. Accessing Web Pages
      3. 16.5.3. Sending and Receiving E-mail Messages
    6. 16.6. Networking with Lua and Streams
      1. 16.6.1. On the Server Side: inetd and Friends
      2. 16.6.2. On the Client Side: ssh and Friends
    7. 16.7. Summary
    8. 16.8. Exercises
  22. 17. Programming Games with Lua
    1. 17.1. Understanding Why and When to Use Lua
    2. 17.2. Simple 2-D Action Game Using SDL
      1. 17.2.1. Installing SDL and LuaCheia
      2. 17.2.2. Using SDL
    3. 17.3. Summary
    4. 17.4. Exercise
  23. 18. Carrying Lua with You
    1. 18.1. Getting Started with Plua
      1. 18.1.1. Obtaining Plua
      2. 18.1.2. Examining the Distribution Contents
    2. 18.2. Exploring Plua's Features
      1. 18.2.1. Running the Plua Application
      2. 18.2.2. Saving Plua Programs
      3. 18.2.3. Reading the Online Documentation
      4. 18.2.4. Using Palm OS Streams
      5. 18.2.5. Compiling Applications
      6. 18.2.6. Compiling Libraries
    3. 18.3. Plua on the Mothership
      1. 18.3.1. The Command-Line Compiler
      2. 18.3.2. The Palm OS Emulator
      3. 18.3.3. The Palm OS Simulator
    4. 18.4. Programming with Plua
      1. 18.4.1. Generating Graphics
      2. 18.4.2. Programming the User Interface
      3. 18.4.3. Accessing Databases
    5. 18.5. Summary
    6. 18.6. Exercises
  24. 19. Fitting into the Lua Community
    1. 19.1. The Lua Web Site
    2. 19.2. The Lua Reference Manual
    3. 19.3. Framing Questions
    4. 19.4. The Lua Mailing List
      1. 19.4.1. Viewing and Searching the Archives
      2. 19.4.2. Downloading the Archives
      3. 19.4.3. Using a Web Browser to Access the List
      4. 19.4.4. Using a Newsreader to Access the List
      5. 19.4.5. Subscribing to the List Server
      6. 19.4.6. Posting Messages
    5. 19.5. The Lua Chat Room
    6. 19.6. Forums
    7. 19.7. The Lua Wiki
    8. 19.8. LuaForge
    9. 19.9. Annual Workshops
    10. 19.10. Summary
  25. A. Answers
    1. A.1. Chapter 2
      1. A.1.1. Exercise 1 Solution
      2. A.1.2. Exercise 2 Solution
      3. A.1.3. Exercise 3 Solution
      4. A.1.4. Exercise 4 Solution
      5. A.1.5. Exercise 5 Solution
    2. A.2. Chapter 3
      1. A.2.1. Exercise 1 Solution
      2. A.2.2. Exercise 2 Solution
      3. A.2.3. Exercise 3 Solution
      4. A.2.4. Exercise 4 Solution
      5. A.2.5. Exercise 5 Solution
    3. A.3. Chapter 4
      1. A.3.1. Exercise 1 Solution
      2. A.3.2. Exercise 2 Solution
      3. A.3.3. Exercise 3 Solution
      4. A.3.4. Exercise 4 Solution
      5. A.3.5. Exercise 5 Solution
    4. A.4. Chapter 5
      1. A.4.1. Exercise 1 Solution
      2. A.4.2. Exercise 2 Solution
      3. A.4.3. Exercise 3 Solution
      4. A.4.4. Exercise 4 Solution
      5. A.4.5. Exercise 5 Solution
      6. A.4.6. Exercise 6 Solution
      7. A.4.7. Exercise 7 Solution
    5. A.5. Chapter 6
      1. A.5.1. Exercise 1 Solution
      2. A.5.2. Exercise 2 Solution
      3. A.5.3. Exercise 3 Solution
    6. A.6. Chapter 7
      1. A.6.1. Exercise 1 Solution
      2. A.6.2. Exercise 2 Solution
    7. A.7. Chapter 8
      1. A.7.1. Exercise 1 Solution
      2. A.7.2. Exercise 2 Solution
    8. A.8. Chapter 9
      1. A.8.1. Exercise 1 Solution
      2. A.8.2. Exercise 2 Solution
      3. A.8.3. Exercise 3 Solution
    9. A.9. Chapter 10
      1. A.9.1. Exercise 1 Solution
      2. A.9.2. Exercise 2 Solution
    10. A.10. Chapter 12
      1. A.10.1. Exercise 1 Solution
      2. A.10.2. Exercise 2 Solution
    11. A.11. Chapter 13
      1. A.11.1. Exercise 1 Solution
      2. A.11.2. Exercise 2 Solution
      3. A.11.3. Exercise 3 Solution
    12. A.12. Chapter 14
      1. A.12.1. Exercise 1 Solution
      2. A.12.2. Exercise 2 Solution
    13. A.13. Chapter 15
      1. A.13.1. Exercise 1 Solution
      2. A.13.2. Exercise 2 Solution
      3. A.13.3. Exercise 3 Solution
    14. A.14. Chapter 16
      1. A.14.1. Exercise 1 Solution
      2. A.14.2. Exercise 2 Solution
      3. A.14.3. Exercise 3 Solution
    15. A.15. Chapter 17
      1. A.15.1. Exercise Solution
    16. A.16. Chapter 18
      1. A.16.1. Exercise 1 Solution
      2. A.16.2. Exercise 2 Solution
      3. A.16.3. Exercise 3 Solution
O'Reilly logo

Chapter 15. Programming for the Web

Although the Internet is the infrastructure for a myriad of protocols, e-mail and the Web are the two services most people associate with it. In this chapter, you'll learn how nicely Lua fits into the Web part of the equation. In this chapter, you learn about the following:

  • The basic operation of web servers

  • Generating web content dynamically with Lua

  • Issues with serving dynamic content

  • Handling input from users by means of forms

  • The basics of the Lua-based Kepler web server environment

Along the way, you'll see how Lua can make use of community libraries to dynamically generate attractive, informative web pages.

A Web Server Primer

To generate web pages using Lua, you should have a comfortable grasp on how web servers operate. This understanding will let you develop Lua programs that focus on the Web's strengths and deal with the Web's limitations in as graceful a manner as possible. As you'll see in this chapter, a successful web application draws on many different tools, with the web server itself playing the central role.

Conceptually, a web server is pretty simple. It's an application that runs on a host and listens for inbound connections. It has access to resources, generally web pages, that are handed out in response to requests made by other applications, usually web browsers. These requests and responses conform to a standard known as hypertext transfer protocol, or HTTP. Unlike many other protocols that define how two applications can have an ...

The best content for your career. Discover unlimited learning on demand for around $1/day.