gpt4 book ai didi

lua - 为什么 lua 解释器中的 -l 选项行为奇怪?

转载 作者:行者123 更新时间:2023-12-01 05:48:37 25 4
gpt4 key购买 nike

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.



以上来自以下链接: https://www.lua.org/pil/1.1.html .然而,当我尝试它时,我遇到了语法错误。

所以,在文件 a.lua 中,我只有一行,即 a=1 .然后在文件 b.lua 中,我也只有一行 print("the value of a is:",a) .然后,
:~$ lua -i -la -lb
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
the value of a is: true
>
:~$

:~$ lua -la -lb
the value of a is: true
Lua 5.2.4 Copyright (C) 1994-2015 Lua.org, PUC-Rio
>

为什么它打印出“a 的值是:true”?而不是“a的值是:1”?
非常感谢任何评论。

最佳答案

错误是因为您正在使用 a = 1在您的文件中 a.lua ,与示例中的 a.lua 不同包含 x = 1b.lua包含 print(x) .
使用 a而不是 x意味着您使用相同的 a -la 时写入的变量完成,将其更改为 true .
发生这种情况是因为选项 -l name等于 name = require("name") .当 require 完成时,对于没有返回结果的文件,require 将返回 true .
您的命令 lua -la -lb lua 看起来像这样:

a = require("a") --this returning true after it completes
b = require("b") --this printing the value of `a` which will always be `true`
资料来源:

Similar question answered on Lua-Users: command-line -l option issue

Egor Skriptunoff's Comment on this question

关于lua - 为什么 lua 解释器中的 -l 选项行为奇怪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58716372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com