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
STDOUTandSTDERRseparately - read
STDOUTandSTDERRan 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.
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.