在学习Lua require关键字时,出现了一个错误“/usr/local/bin/lua: ./require_func.lua:4: attempt to index a function value (local 'mytest')”,如:
两个脚本内容如下
testRequire.lua
print("load file testRequest.lua")
function testRq(x)
print("in file testRequire.lua, input param is "..x)
end
return testRq
require_func.lua
#!/usr/local/bin/lua
local mytest = require("testRequire")
mytest.testRq("22")
?这样调用是有问题的,因为require后,mytest已经是一个函数了,如修改成这样:
#!/usr/local/bin/lua
local mytest = require("testRequire")
print(type(mytest))
因为返回的已经是函数,所以require后就可以直接函数调用,修改如下:
?
#!/usr/local/bin/lua
local mytest = require("testRequire")
print(type(mytest))
mytest("11234343")
?
|