7 – Lua独立
4 分钟阅读
7 – Lua Standalone – Lua独立
Although Lua has been designed as an extension language, to be embedded in a host C program, it is also frequently used as a standalone language. An interpreter for Lua as a standalone language, called simply lua
, is provided with the standard distribution. The standalone interpreter includes all standard libraries. Its usage is:
尽管 Lua 被设计为一种扩展语言,可以嵌入到宿主 C 程序中,但它也经常被用作一种独立语言。一个独立语言的 Lua 解释器,简单称为 lua
,随标准发行版提供。独立解释器包含所有标准库。它的用法是:
lua [options] [script [args]]
lua [选项] [脚本 [参数]]
The options are:
选项有:
-e \*stat\*
: execute string stat;-e *stat*
:执行字符串 stat;-i
: enter interactive mode after running script;-i
:运行脚本后进入交互模式;-l \*mod\*
: “require” mod and assign the result to global mod;-l *mod*
:“require” 模块并将结果分配给全局模块;-l \*g=mod\*
: “require” mod and assign the result to global g;-l *g=mod*
:“require” 模块并将结果分配给全局 g;-v
: print version information;-v
:打印版本信息;-E
: ignore environment variables;-E
:忽略环境变量;-W
: turn warnings on;-W
:打开警告;--
: stop handling options;--
:停止处理选项;-
: executestdin
as a file and stop handling options.-
:将stdin
作为文件执行并停止处理选项。
(The form -l *g=mod*
was introduced in release 5.4.4.)
( -l *g=mod*
表单在 5.4.4 版本中引入。)
After handling its options, lua
runs the given script. When called without arguments, lua
behaves as lua -v -i
when the standard input (stdin
) is a terminal, and as lua -
otherwise.
在处理其选项后, lua
运行给定的脚本。当在没有参数的情况下调用时, lua
在标准输入 ( stdin
) 是终端时表现为 lua -v -i
,否则表现为 lua -
。
When called without the option -E
, the interpreter checks for an environment variable LUA_INIT_5_4
(or LUA_INIT
if the versioned name is not defined) before running any argument. If the variable content has the format @*filename*
, then lua
executes the file. Otherwise, lua
executes the string itself.
当在没有选项 -E
的情况下调用时,解释器在运行任何参数之前检查环境变量 LUA_INIT_5_4
(如果未定义版本化名称,则检查 LUA_INIT
)。如果变量内容具有格式 @*filename*
,则 lua
执行该文件。否则, lua
执行字符串本身。
When called with the option -E
, Lua does not consult any environment variables. In particular, the values of package.path
and package.cpath
are set with the default paths defined in luaconf.h
.
当使用选项 -E
调用时,Lua 不会查询任何环境变量。特别是, package.path
和 package.cpath
的值使用 luaconf.h
中定义的默认路径设置。
The options -e
, -l
, and -W
are handled in the order they appear. For instance, an invocation like
选项 -e
、 -l
和 -W
按其出现的顺序处理。例如,类似于以下内容的调用
$ lua -e 'a=1' -llib1 script.lua
will first set a
to 1, then require the library lib1
, and finally run the file script.lua
with no arguments. (Here $
is the shell prompt. Your prompt may be different.)
将首先将 a
设置为 1,然后需要库 lib1
,最后运行文件 script.lua
,不带任何参数。(此处 $
是 shell 提示符。您的提示符可能不同。)
Before running any code, lua
collects all command-line arguments in a global table called arg
. The script name goes to index 0, the first argument after the script name goes to index 1, and so on. Any arguments before the script name (that is, the interpreter name plus its options) go to negative indices. For instance, in the call
在运行任何代码之前, lua
会将所有命令行参数收集到一个名为 arg
的全局表中。脚本名称转到索引 0,脚本名称后的第一个参数转到索引 1,依此类推。脚本名称之前的任何参数(即解释器名称及其选项)转到负索引。例如,在调用中
$ lua -la b.lua t1 t2
the table is like this:
表格如下所示:
arg = { [-2] = "lua", [-1] = "-la",
[0] = "b.lua",
[1] = "t1", [2] = "t2" }
arg = { [-2] = "lua", [-1] = "-la",
[0] = "b.lua",
[1] = "t1", [2] = "t2" }
If there is no script in the call, the interpreter name goes to index 0, followed by the other arguments. For instance, the call
如果调用中没有脚本,解释器名称将转到索引 0,然后是其他参数。例如,调用
$ lua -e "print(arg[1])"
will print “-e
”. If there is a script, the script is called with arguments arg[1]
, ···, arg[#arg]
. Like all chunks in Lua, the script is compiled as a variadic function.
将打印 " -e
“。如果有脚本,则使用参数 arg[1]
、···、 arg[#arg]
调用脚本。与 Lua 中的所有块一样,脚本被编译为可变参数函数。
In interactive mode, Lua repeatedly prompts and waits for a line. After reading a line, Lua first try to interpret the line as an expression. If it succeeds, it prints its value. Otherwise, it interprets the line as a statement. If you write an incomplete statement, the interpreter waits for its completion by issuing a different prompt.
在交互模式下,Lua 重复提示并等待一行。在读取一行后,Lua 首先尝试将该行解释为表达式。如果成功,它将打印其值。否则,它将把该行解释为语句。如果您编写了一个不完整的语句,解释器将通过发出不同的提示来等待其完成。
If the global variable _PROMPT
contains a string, then its value is used as the prompt. Similarly, if the global variable _PROMPT2
contains a string, its value is used as the secondary prompt (issued during incomplete statements).
如果全局变量 _PROMPT
包含一个字符串,则其值用作提示。同样,如果全局变量 _PROMPT2
包含一个字符串,则其值用作辅助提示(在不完整语句期间发出)。
In case of unprotected errors in the script, the interpreter reports the error to the standard error stream. If the error object is not a string but has a metamethod __tostring
, the interpreter calls this metamethod to produce the final message. Otherwise, the interpreter converts the error object to a string and adds a stack traceback to it. When warnings are on, they are simply printed in the standard error output.
脚本中出现未保护的错误时,解释器会将错误报告给标准错误流。如果错误对象不是字符串,而是具有元方法 __tostring
,解释器会调用此元方法来生成最终消息。否则,解释器会将错误对象转换为字符串,并添加栈回溯。当警告开启时,它们会简单地打印在标准错误输出中。
When finishing normally, the interpreter closes its main Lua state (see lua_close
). The script can avoid this step by calling os.exit
to terminate.
正常结束时,解释器会关闭其主 Lua 状态(参见 lua_close
)。脚本可以通过调用 os.exit
来终止,从而避免此步骤。
To allow the use of Lua as a script interpreter in Unix systems, Lua skips the first line of a file chunk if it starts with #
. Therefore, Lua scripts can be made into executable programs by using chmod +x
and the #!
form, as in
为了允许在 Unix 系统中将 Lua 用作脚本解释器,如果文件块的第一行以 #
开头,Lua 会跳过该行。因此,可以通过使用 chmod +x
和 #!
形式将 Lua 脚本制成可执行程序,如下所示: 当然,Lua 解释器的位置在您的机器上可能不同。如果 #
在您的 chmod +x
中,那么
#!/usr/local/bin/lua
Of course, the location of the Lua interpreter may be different in your machine. If lua
is in your PATH
, then
#!/usr/bin/env lua
#!/usr/bin/env lua
是一个更便携的解决方案。
is a more portable solution.