你如何用lua构建一个读写管道? [英] How do you construct a read-write pipe with lua?
本文介绍了你如何用lua构建一个读写管道?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想做相当于:
foo=$(echo "$foo"|someprogram)
在 lua 中——即,我有一个包含一堆文本的变量,我想通过过滤器(在 python 中实现)来运行它.
within lua -- ie, I've got a variable containing a bunch of text, and I'd like to run it through a filter (implemented in python as it happens).
有什么提示吗?
补充:真的很想在不使用临时文件的情况下做到这一点
Added: would really like to do this without using a temporary file
推荐答案
啊哈,一个可能更好的解决方案:
Aha, a possibly better solution:
require('posix')
require('os')
require('io')
function splat_popen(data,cmd)
rd,wr = posix.pipe()
io.flush()
child = posix.fork()
if child == 0 then
rd:close()
wr:write(data)
io.flush()
os.exit(1)
end
wr:close()
rd2,wr2 = posix.pipe()
io.flush()
child2 = posix.fork()
if child2 == 0 then
rd2:close()
posix.dup(rd,io.stdin)
posix.dup(wr2,io.stdout)
posix.exec(cmd)
os.exit(2)
end
wr2:close()
rd:close()
y = rd2:read("*a")
rd2:close()
posix.wait(child2)
posix.wait(child)
return y
end
munged=splat_popen("hello, world","/usr/games/rot13")
print("munged: "..munged.." !")
这篇关于你如何用lua构建一个读写管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文