This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The third edition targets Lua 5.2 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.
|Programming in Lua|
|Part I. The Language Chapter 1. Getting Started|
Each piece of code that Lua executes, such as a file or a single line in interactive mode, is a chunk. More specifically, a chunk is simply a sequence of statements.
A semicolon may optionally follow any statement. Usually, I use semicolons only to separate two or more statements written in the same line, but this is just a convention. Line breaks play no role in Lua's syntax; for instance, the following four chunks are all valid and equivalent:
a = 1 b = a*2 a = 1; b = a*2; a = 1 ; b = a*2 a = 1 b = a*2 -- ugly, but valid
A chunk may be as simple as a single statement, such as in the "hello world" example, or it may be composed of a mix of statements and function definitions (which are assignments actually, as we will see later), such as the factorial example. A chunk may be as large as you wish. Because Lua is used also as a data-description language, chunks with several megabytes are not uncommon. The Lua interpreter has no problems at all with large sizes.
Instead of writing your program to a file, you may run the stand-alone interpreter in interactive mode. If you call Lua without any arguments, you will get its prompt:
Lua 5.0 Copyright (C) 1994-2003 Tecgraf, PUC-Rio >Thereafter, each command that you type (such as
print "Hello World") executes immediately after you press
<enter>. To exit the interactive mode and the interpreter, just type end-of-file (
ctrl-Zin DOS/Windows), or call the
exitfunction, from the Operating System library (you have to type
In interactive mode, Lua usually interprets each line that you type
as a complete chunk.
However, if it detects that the line cannot form a complete chunk,
it waits for more input,
until it has a complete chunk.
When Lua is waiting for a line continuation,
it shows a different prompt (typically
Therefore, you can enter a multi-line definition,
such as the
directly in interactive mode.
it is more convenient to put such definitions in a file,
and then call Lua to run that file.
You can execute a sequence of chunks by giving them all as arguments
to the stand-alone interpreter, with the
if you have a file
a with a single statement
and another file
b with the statement
the command line
prompt> lua -la -lbwill run the chunk in
a, then the one in
b, which will print the expected 1. (The
-loption actually calls
require, which looks for the files in a specific path. So, the previous example will not work if this path does not include the current directory. We will discuss the
requirefunction in more details in Section 8.1.)
You may use the
-i option to instruct Lua to
start an interactive session after running the given chunks.
A command line like
prompt> lua -i -la -lbwill run the chunk in
a, then the one in
b, and then prompt you for interaction. This is especially useful for debugging and manual testing. At the end of this chapter we will see other options for the stand-alone interpreter.
Another way to link chunks is with the
which immediately executes a file.
For instance, you may have a file
-- file 'lib1.lua' function norm (x, y) local n2 = x^2 + y^2 return math.sqrt(n2) end function twice (x) return 2*x endThen, in interactive mode, you can type
> dofile("lib1.lua") -- load your library > n = norm(3.4, 1.0) > print(twice(n)) --> 7.0880180586677
dofile function is useful also when you are
testing a piece of code.
You can work with two windows:
One of them is a text editor with your program
(in a file
and the other is a console running Lua in interactive mode.
After saving a modification that you make to your program,
dofile("prog.lua") in the Lua console to
load the new code;
then you can exercise the new code,
calling its functions and printing the results.
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|