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.
|Programming in Lua|
|Part I. The Language Chapter 1. Getting Started|
The stand-alone interpreter
lua.c due to its source file,
lua due to its executable)
is a small program that allows the direct use of Lua.
This section presents its main options.
When the interpreter loads a file,
it ignores its first line if that line starts with a number sign (`
That feature allows the use of Lua as a script interpreter
in Unix systems.
If you start your program with something like
#!/usr/local/bin/lua(assuming that the stand-alone interpreter is located at
#!/usr/bin/env luathen you can call the program directly, without explicitly calling the Lua interpreter.
The usage of
lua [options] [script [args]]Everything is optional. As we have seen already, when we call
luawithout arguments the interpreter enters in interactive mode.
-e option allows us to enter
code directly into the command line.
prompt> lua -e "print(math.sin(12))" --> -0.53657291800043(Unix needs the double quotes to stop the shell from interpreting the parentheses.) As we previously saw,
-lloads a file and
-ienters interactive mode after running the other arguments. So, for instance, the call
prompt> lua -i -l a.lua -e "x = 10"will load the file
a.lua, then execute the assignment
x = 10, and finally present a prompt for interaction.
Whenever the global variable
_PROMPT is defined,
lua uses its value as the prompt when interacting.
So, you can change the prompt with a call like this:
prompt> lua -i -e "_PROMPT=' lua> '" lua>We are assuming that
"prompt"is the system's prompt. In the example, the outer quotes stop the shell from interpreting the inner quotes, which are interpreted by Lua. More exactly, Lua receives the following command to run:
_PROMPT=' lua> 'which assigns the string
" lua> "to the global variable
Before it starts running arguments,
lua looks for an environment variable called
If there is such a variable and its content is
lua loads the given file.
LUA_INIT is defined but does not start with `
lua assumes that it contains Lua code and runs it.
This variable gives you great power when configuring the
because you have the full power of Lua in the configuration.
You can pre-load packages,
change the prompt and the path,
define your own functions,
rename or delete functions, and so on.
A main script can retrieve its arguments in the global variable
In a call like
prompt> lua script a b c
luacreates the table
argwith all the command-line arguments, before running the script. The script name goes into index 0; its first argument (
ain the example), goes to index 1, and so on. Eventual options go to negative indices, as they appear before the script. For instance, in the call
prompt> lua -e "sin=math.sin" script a b
luacollects the arguments as follows:
arg[-3] = "lua" arg[-2] = "-e" arg[-1] = "sin=math.sin" arg = "script" arg = "a" arg = "b"More often than not, the script only uses the positive indices (
arg, in the example).
|Copyright © 2003–2004 Roberto Ierusalimschy. All rights reserved.|