这个方式是从openwrt的maillist里找到的,适合那些想修改luci,但是对lua语言又不熟悉的人。
大意如下:
某人问在LuCI里添加print("hello"),出现错误。为啥?
attempt to call global 'print' (a nil value)
然后,下面有个人回复说,需要在文件开头添加这么一行:
local print = print
因为这个文件是一个module,它调用了module函数。详细信息,可以参考Lua手册
原文如下:
Hi all, when i add above 'print("hello")' in /usr/lib/lua/luci/model/uci.lua got follow error: /usr/lib/lua/luci/dispatcher.lua:448: Failed to execute call dispatcher target for entry '/servicectl/restart/firewall'. The called action terminated with an exception: /usr/lib/lua/luci/model/uci.lua:71: attempt to call global 'print' (a nil value) stack traceback: [C]: in function 'assert' /usr/lib/lua/luci/dispatcher.lua:448: in function 'dispatch' /usr/lib/lua/luci/dispatcher.lua:195: in function </ usr/lib/lua/luci/dispatcher.lua:194> why cannot use print to print some value? thanks all! elvis -------------------------------------------------------------------------------------- You need to define it first at the top of the file: diff --git a/libs/core/luasrc/model/uci.lua b/libs/core/luasrc/model/uci.lua index a394563..f513c77 100644 --- a/libs/core/luasrc/model/uci.lua +++ b/libs/core/luasrc/model/uci.lua @@ -27,6 +27,7 @@ local os = require "os" local uci = require "uci" local util = require "luci.util" local table = require "table" +local print = print Why? It's because of the following function call: module "luci.model.uci" For the details, read the fine Lua manual[1]. BTW, it's better to use file for debugging output, since you can easily loose some hairs with stdout/stderr debugging in the Luci development :-) 1. http://www.lua.org/manual/5.1/manual.html#pdf-module