lua - 为什么我在我的 lua 函数中看不到打印

标签 lua

我的问题有多个部分,但首先这是我的示例 lua 代码 (test.lua):

local traceback = debug.traceback

local inspect = require('inspect')

local foo = "function nop(); print('this is war'); return true; end"

local f = loadstring(foo)

local result = f()

print(result)

local status, val= xpcall(function () return f() end,debug.traceback)

print('status .. ' .. tostring(status))

print(val)

所以,

  1. 当我运行 local result = f() 时。我仍然看到 result 值为 nil

  2. 当函数 f 被执行时。为什么我看不到 print o/p。

最佳答案

您加载到 f 中的 foo 中的代码块不会返回任何值。

它定义了一个函数,仅此而已。

如果你想在运行时从 block 中返回函数,你需要将 return nop 添加到字符串的末尾。

关于lua - 为什么我在我的 lua 函数中看不到打印,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32272377/

相关文章:

NGINX LUA 内容长度 +1 字节丢失

c++ - 如何卸载/销毁lua脚本?

nginx - 在 Nginx 配置中运行 Lua?

带命令行参数的 Lua os.execute

lua - 如何避免 init 函数失败时注册 box.once ('init' , function ...) ?

lua - WoW 插件 - 按下按键时的动态工具提示

错误 : attempt to call a nil value (field 'getn' )

c++ - Lua中看起来像table的函数有哪些?

parsing - 如何让 ANTLR 输出分层 AST?

c - Lua 在 PPC Micro 上的足迹