您可以在处理程序的范围内定义 OutToUser:
function Server:init()
local function handler(skt, host, port)
--make the function local to here
local function OutToUser(data)
--references the skt variable in the enclosing scope
--(the handler function)
skt:send(data .. "\r\n")
end
while true do
data = skt:receive()
if data == "quit" then
OutToUser(data)
end
end
end
local server = socket.bind("*", 49796)
copas.addserver(server,
function(c) return handler(copas.wrap(c), c:getpeername()) end
)
copas.loop()
end
函数始终可以引用其范围内的变量(函数参数和使用local 声明的变量),即使它们已离开该范围 - 您可以将其用作替代解决方案,您可以将其括起来 您希望函数在函数外的范围内使用的变量:
local function makeOTU(skt)
--skt is visible in the scope of the function
--that gets returned as a result
return function(data)
skt:send(data .. "\r\n")
end
end
function Server:init()
local function handler(skt, host, port)
--create a function that references skt
--as part of its closure
local OutToUser = makeOTU(skt)
while true do
data = skt:receive()
if data == "quit" then
-- OutToUser is still referencing the
-- skt from the call to makeOTU()
OutToUser(data)
end
end
end
local server = socket.bind("*", 49796)
copas.addserver(server,
function(c) return handler(copas.wrap(c), c:getpeername()) end
)
copas.loop()
end
请注意在这两个示例中使用 local 关键字:如果您忽略 local,名称将完全忽略范围并进入/来自 the global environment(这只是一个像任何其他:当你调用一个新的 Lua 状态时,它被放置在全局 _G) 中,这不是你想要的。
将变量保持在其范围内而不是使用全局变量很重要。以这两个函数为例:
local function yakkity(file, message)
line = message .. '\n' --without local,
--equivalent to _G["line"] = message
function yak() --without local,
--equivalent to _G["yak"] = function()
file:write(line) --since no local "line" is defined above,
--equivalent to file:write(_G["line"])
end
for i=1, 5 do
yak()
end
end
local function yakker(file, message)
line = message .. '\n' --without local,
--equivalent to _G["line"] = message
return function()
file:write(line) --again, since no local "line" is defined above,
--equivalent to file:write(_G["line"])
end
end
因为他们的变量没有被定义为本地变量,所以他们会破坏彼此的数据,把他们的财物放在任何人都可以滥用的地方,而且通常表现得像个懒汉:
--open handles for two files we want:
local yesfile = io.open ("yesyes.txt","w")
local nofile = io.open ("no.txt","w")
--get a function to print "yes!" - or, rather,
--print the value of _G["line"], which we've defined to "yes!".
--We'll see how long that lasts...
local write_yes = yakker(yesfile,"yes!")
--Calling write_yes() now will write "yes!" to our file.
write_yes()
--when we call yakkity, though, it redefines the global value of "line"
--(_G["line"]) - as well as defining its yak() function globally!
--So, while this function call does its job...
yakkity(nofile, "oh no!")
--calling write_yes(), which once again looks up the value of _G["line"],
--now does the exact OPPOSITE of what it's supposed to-
write_yes() --this writes "oh no!" to yesfile!
--additionally, we can still write to the nofile handle we passed to yakkity()
--by calling the globally-defined yak() function!
yak() --this writes a sixth "oh no!" to nofile!
--even once we're done with the file and close our handle to it...
nofile:close()
--yak() still refers to that handle and will still try to write to it!
yak() --tries to write to the closed file handle and throws an error!