2

我不确定在哪里可以看到我可以用来捕获另一个程序中加载的程序中的变量更改。

这是我的代码,虽然很乱:

function launch()
    shell.run("clear")
    print("Preparing for first time run.")
    sleep(1)
    print("")
    local program = netTest()
    local file = loadstring(program)
    file()
    sleep(3)
    shell.run("clear")
end

function netTest()
  local output = http.get("http://pastebin.com/raw.php?i=hzZv3YH2")
  if output then
        local contents = output.readAll()
        output.close()
        return contents
  else
        print("Empty response")
        return false
  end
end

local program = netTest()
local file = loadstring(program)

launch()

这是它调用的代码:

function ping()
fails = 0
pingResult = 0
print("Testing Internet Connection.")
print("")
oldx, oldy = term.getCursorPos()
print("Connection Test 1")
http.request("http://www.google.com/")
        if os.pullEvent() == "http_success" then
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Passed")
        else
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Failed")
                fails = fails+1
        end
sleep(1)
print("Connection Test 2")
http.request("http://www.microsoft.com/")
        if os.pullEvent() == "http_success" then
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Passed")
        else
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Failed")
                fails = fails+1
        end
sleep(1)
print("Connection Test 3")
http.request("http://www.example-failure.com/")
        if os.pullEvent() == "http_success" then
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Passed")
        else
                local oldx, oldy = term.getCursorPos()
                term.setCursorPos(46,oldy-1)
                io.write("Failed")
                fails = fails+1
        end
sleep(1)
        if fails == 0 then
                print("")
                print("")
                print("Test Complete, no failures detected.")
                sleep(1.5)
        elseif fails == 1 then
                print("")
                print("")
                print("1 connection failures detected. A Website Host might be down however connectivity is still there.")
                print("")
                print("Test Complete.")
                sleep(1.5)
        elseif fails == 2 then
                print("")
                print("")
                print("2 connection failures detected. Possible limited web connectivity.")
                print("")
                print("Test Complete.")
                sleep(1.5)
        elseif fails == 3 then
                print("")
                print("")
                print("Catastrophic connection failure detected. A firewall or improper internet settings may be the problem.")
                print("")
                print("Test Complete.")
                pingResult = __pingResult + 3
                sleep(1.5)
        end
end

ping()

正如您所看到的,它在外部运行一个程序,该程序将通过向几个页面发出 http 请求来测试连接,以确保连接到 Internet。(我知道,这有点蹩脚,但我还在学习)。

基本上,如果连接在所有 3 个阶段都读取失败,它将使我的 pingResult 变量 = 3。我想要从第一个调用我的 Internet 实用程序的程序中做的是记录该变量的设置。如果在关闭时将其设置为 3,则记录该变量的设置,然后为简单起见,打印该变量,以便我可以看到它是 0 或 3。我稍后会用它做一些事情,但你会明白它的要点.

我已经尝试了其他几件事,但似乎无法弄清楚我是如何做到这一点的。看到我还是新手,我尝试了随机的东西,但似乎没有任何效果,或者我只是做错了,所以我不知道该怎么做。这几天一直在尝试,但没有成功。

4

1 回答 1

3

首先,您发布的大部分代码都是噪音——打印语句、光标操作、从 pastebin(?)获取 Lua 代码,各种与您的问题完全无关的逻辑,包括实际上什么都不做的代码。这就是为什么你还没有得到回应。

请参阅:如何以聪明的方式提问:数量不是精确的


如果我们从您的帖子中删除所有无关的内容,我们将得到:

-- main.lua
local file = loadfile('nettest.lua')
file()

-- nettest.lua
pingResult = 123

现在来解决您的问题:

如您所见,它正在做的是在外部运行程序:

它没有在外部运行任何东西。您已经获取了外部代码,但它是在本地执行的。您可以看到代码对全局状态所做的任何更改。在这种情况下, main.lua 可以访问pingResult. 例如,您可以编写:

-- main.lua
local file = loadfile('nettest.lua')
file()
print(pingResult)

当然,如果您想在脚本和 ping 模块之间进行更清晰的分离,您可能应该让该代码返回一个值而不是写入全局:

-- main.lua
local file = loadfile('nettest.lua')
local pingResult = file()
print(pingResult)

-- nettest.lua
local pingResult
-- ... code the computes the result an stores it in pingResult ...
return pingResult
于 2013-02-08T22:55:56.897 回答