Code:
function callRemoteLua(keyword)
print(keyword)
local handler = io.popen("lua fengxianpoem.lua "..keyword)
local poem = handler:read("*all")
handler:close()
print("found answer:"..poem)
end
Run-time error
World: robotcat
Immediate execution
[string "Script file"]:920: 'io.popen' not implemented in MUSHclient
stack traceback:
[C]: in function 'popen'
[string "Script file"]:920: in function 'callRemoteLua'
[string "Script file"]:905: in function 'answerPoem4'
[string "触发器: "]:1: in main chunk
脚本错误处的上下文:
916 : end
917 :
918 : function callRemoteLua(keyword)
919 : print(keyword)
920*: local handler = io.popen("lua fengxianpoem.lua "..keyword)
921 : local poem = handler:read("*all")
922 : handler:close()
923 : print("found answer:"..poem)
924 : end
Quote: io.popen is not supported under the version compiled into MUSHclient
Try this instead:
-- replacement for io.popen
-- or os.execute with output
function osexecute(cmd)
local n = GetInfo(66).."osexecute_temp_file.txt" -- temp file for catching output
cmd = cmd .. " > \""..n.."\""
local err = os.execute(cmd)
local messages = {}
for line in io.lines(n) do
table.insert(messages, line)
end
os.remove(n) -- remove temp file
return err, messages
end
But wait!
Are you calling another Lua from inside Lua? Why on earth would you do that?
Why not just import fengxianpoem.lua and call its functions?
I have tried but output nothing to the temp file.
Its size is zero.
But when I run the code in command line,it output correct result.
Maybe it is because of asynchronize http request.
I don't know the root cause.
I've been trying to call luaiconv myself. Without success. Which characters sets are you translating from/to?
If you can require luaiconv from the command line you should be able to do it from your MUSHclient script. Maybe it isn't in your path or something like that.
I want to translate text between gbk and utf8 .
The text from mud is encoded in GBK but I handle It in lua in utf8
It works in Windows command line
I have installed lua5.1 by full package with an installer
1.demo.lua
First,call fengxianpoem to fetech some html page and parse it by keyword. and write the result into a local text file.
Second,read the text file for result.
Full path of file or lua is required for mushclient,otherwise it failed.
function getScriptFilePath(fileName)
local mushSupport = true
if mushSupport then
return GetInfo(66).."scripts\\"..fileName
else
return fileName
end
end
function callRemoteFengxianLua(keyword)
keyword = string.gsub(keyword,"。","")
keyword = string.gsub(keyword,",",",")
print(keyword)
--Use fullpath
local script = getScriptFilePath("fengxianpoem.lua")
local resultPath = getScriptFilePath("fengxianpoemresult.txt")
local luacmd = "C:\\Program\ Files\\Lua\\5.1\\lua"
--lua shell command
local err = os.execute("\""..luacmd.."\" "..script.." "..keyword.." "..resultPath)
print("os.execute result:"..err)
--read result
local fileHandle = assert(io.open(resultPath,"r"),"failed to open file")
if fileHandle then
local dataUTF8 = fileHandle:read("*a");
print(dataUTF8)
end
fileHandle:close()
end
2.Fengxianpoem.lua
Fetech some data and parse it by keyword,save the result into a text file.
local keyword = "You failed when you get these words"
local filePath --full file path for writing result
if #arg>1 then
filePath = arg[2]
keyword = arg[1]
--print("keyword:"..keyword)
--print("filePath:"..filePath)
-- answerPoem5 fetech http page and parse it to result text,and write it to file.
----***luaiconv**** is called in this function.This is what I want.
---The lua script is running in command line so it works like that in command line.
local result = answerPoem5(keyword)
if not result then
result = ""
end
local fileHandle = assert(io.open(filePath,"w+"),"failed to open file")
--delay()
if fileHandle then
print("result:"..result)
fileHandle:write(result)
--delay()
fileHandle:close()
end
end
Thanks for your support.
I don't get require "luaiconv.dll" work yet.
And have no idea now.