1 – Getting Started
To keep with the tradition, our first program in Lua just prints «Hello World» :
If you are using the stand-alone Lua interpreter, all you have to do to run your first program is to call the interpreter (usually named lua ) with the name of the text file that contains your program. For instance, if you write the above program in a file hello.lua , the following command should run it:
As a slightly more complex example, the following program defines a function to compute the factorial of a given number, asks the user for a number, and prints its factorial:
-- defines a factorial function function fact (n) if n == 0 then return 1 else return n * fact(n-1) end end print("enter a number:") a = io.read("*number") -- read a number print(fact(a))
If you are using Lua embedded in an application, such as CGILua or IUPLua, you may need to refer to the application manual (or to a «local guru») to learn how to run your programs. Nevertheless, Lua is still the same language; most things that we will see here are valid regardless of how you are using Lua. For a start, we recommend that you use the stand-alone interpreter (that is, the lua executable) to run your first examples and experiments.
Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved. |
Programming in Lua
This book is a detailed and authoritative introduction to all aspects of Lua programming written by Lua’s chief architect.
Programming in Lua provides a solid base to any programmer who wants to use Lua. It covers all aspects of Lua—from the basics to its API with C. The book is the main source of programming patterns for Lua, with numerous code examples that help the reader to make the most of Lua’s flexibility and powerful mechanisms. The book is targeted at people with some programming background, but it does not assume any prior knowledge about Lua or other scripting languages.
When you buy a copy of this book, you help to support the Lua project.
For the official definition of the Lua language, see the reference manual.
The fourth edition updates the book to Lua 5.3 and marks a complete reorganization of the text. Building on many years of experience teaching Lua, Roberto has restructured the book to present the material in a growing order of complexity, allowing the reader to better absorb the character of the language.
The book is available at the main online stores and also as an e-book.
The third edition is aimed at Lua 5.2 and can be used with other versions. It brings substantial new material. All chapters include exercises, ranging from simple questions about the language to full small-size projects.
The book is available at the main online stores and also as an e-book.
The second edition was aimed at Lua 5.1 and remains quite relevant for later versions.
The book is still available at the main online stores. It is also available in German, Korean, Chinese, and Japanese.
The first edition was aimed at Lua 5.0 and remains largely relevant for later versions, but there are some differences. It is freely available online for personal use.
Last update: Fri Jul 3 12:43:43 UTC 2020