1.1 – Chunks


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 © 1994-2003 Tecgraf, PUC-Rio

Thereafter, each command that you type (such as

print "Hello World"

) executes immediately after you press


. To exit the interactive mode and the interpreter, just type end-of-file (


in Unix,


in DOS/Windows), or call the


function, 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


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


option. For instance, if you have a file


with a single statement


and another file


with the statement


, the command line
   prompt> lua -la -lb

will run the chunk in


, then the one in


, which will print the expected 1. (The


option actually calls


, 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


You may use the


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


, then the one in


, 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


function, 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)
   function twice (x)
     return 2*x

Then, in interactive mode, you can type
   > dofile("lib1.lua")   -- load your library
   > n = norm(3.4, 1.0)
   > print(twice(n))      --> 7.0880180586677



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


, say) and the other is a console running Lua in interactive mode. After saving a modification that you make to your program, you execute


in the Lua console to load the new code; then you can exercise the new code, calling its functions and printing the results.

Users browsing this thread:
1 Guest(s)