This first edition was written for Lua 5.0. While still largely relevant for later versions, there are some differences.
The fourth edition targets Lua 5.3 and is available at Amazon and other bookstores.
By buying the book, you also help to support the Lua project.


1.1 – Chunks

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-D in Unix, ctrl-Z in DOS/Windows), or call the exit function, from the Operating System library (you have to type os.exit()<enter>).

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 factorial function, directly in interactive mode. Sometimes, however, 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 -l option. For instance, if you have a file a with a single statement x=1 and another file b with the statement print(x), the command line

    prompt> lua -la -lb
will run the chunk in a, then the one in b, which will print the expected 1. (The -l option 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 require function 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 -lb
will 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 dofile function, which immediately executes a file. For instance, you may have a file lib1.lua:

    -- file 'lib1.lua'
    
    function norm (x, y)
      local n2 = x^2 + y^2
      return math.sqrt(n2)
    end
    
    function twice (x)
      return 2*x
    end
Then, in interactive mode, you can type
    > dofile("lib1.lua")   -- load your library
    > n = norm(3.4, 1.0)
    > print(twice(n))      --> 7.0880180586677

The 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 prog.lua, say) and the other is a console running Lua in interactive mode. After saving a modification that you make to your program, you execute 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.