PHP 的 proc_open()、proc_close() 等的 Perl 等价物是什么?
使用 PHP 的 proc_open()
,我可以启动一个进程,在进程运行时使用 fread()
从 STDOUT
和 STDERR
(分别)一次读取任意数量的字节,在 STDOUT
和 STDERR
管道上使用 feof()
检测进程何时完成,然后使用 proc_close() 获取进程的退出代码。我已经用 PHP 完成了所有这些工作。它运作良好,给了我很多的控制权。
有没有办法在 Perl 中完成所有这些事情?总而言之,我需要能够执行以下操作:
- 启动外部进程
- 读取
STDOUT
和STDERR
分别 - 读取
STDOUT
和STDERR
进程运行时一次任意数量的字节(即无需等待进程完成) - 检测进程何时完成
- 获取进程的退出代码
提前感谢您的回答。
Using PHP's proc_open()
, I can start a process, read from STDOUT
and STDERR
(separately) an arbitrary number of bytes at a time using fread()
while the process is running, detect when the process is done using feof()
on the STDOUT
and STDERR
pipes, and then use proc_close()
to get the exit code of the process. I've done all of this in PHP. It works well, and gives me a lot of control.
Is there a way to do all of these things in Perl? To summarize, I need to be able to do the following:
- start an external process
- read
STDOUT
andSTDERR
separately - read
STDOUT
andSTDERR
an arbitrary number of bytes at a time while the process is running (i.e. without having to wait for the process to finish) - detect when the process is finished
- get the exit code of the process
Thanks in advance for your answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用 Perl 的系统调用接口推出您自己的解决方案,但使用内置模块 IPC::Open3 更容易。至于您的列表:
启动外部进程:
分别读取 STDOUT 和 STDERR,一次任意数量的字节:
检测进程何时完成:
获取进程的退出代码:
请务必阅读 IPC::Open3 文档;正如它警告的那样,如果您不小心,当您有单独的 stdout 和 stderr 管道时,很容易陷入僵局。如果子进程填充任一管道,它将阻塞,如果父进程读取其他管道,它将阻塞。
You could roll your own solution using Perl's system call interface, but it's easier to use the built-in module IPC::Open3. As for your list:
Start an external process:
Read STDOUT and STDERR separately, an arbitrary number of bytes at a time:
Detect when the process is finished:
Get the exit code of the process:
Be sure to read the IPC::Open3 documentation; as it warns, it's easy to get yourself deadlocked when you have separate stdout and stderr pipes, if you're not careful. If the child process fills either pipe, it will block, and if the parent process reads the other pipe, it will block.
您需要此模块: IPC::Open3
You want this module: IPC::Open3
你想要IPC::Run,它捕获IO并返回退出值
You want IPC::Run, it captures the IO and returns the exit value