
Chunks
A Lua program consists of chunks, units of compilation.
A chunk may contain subchunks within it, so that they form a
tree-structure. The whole program constitutes a chunk,
the global chunk. All the other chunks are
subchunks of it.
Variables can be declared local to a chunk.
Variables occurring in a chunk which have been declared local
in an enclosing chunk are referred to as upvalues
of the chunk. But note: a variable local to
the global chunk is not quite the same as a global variable,
(see the next section). Local
variables are stored on the stack and are fast to look up.
Always use local variables where possible.
A block of code can be turned into a chunk by being enclosed
between the reserved words
do and end. The body of a function
constitutes a chunk and its parameters are automatically local to it.
Similarly the bodies of looping and conditional structures form
chunks, and the loop variables are automatically local. Code
in a separate file (compiled with loadfile) or in a
string (compiled with load) constitute chunks. They
are compiled as functions of an unknown number of variables.
They can contain return statements.
To understand a program it is important to recognize its
tree-structure of chunks, and to identify the
name-space of each chunk. This explains how
information flows, or is concealed, from one chunk to
another.
