Communicating with external processes
If you are performing a task and encounter a stage where another program does an excellent job, is it possible to call that program and use the results in your D program? The answer is yes. Here, we'll look at using std.process
to run another program and send it input and read its output.
How to do it…
Let's communicate with external processes by executing the following steps:
Import
std.process
.Call
pipeProcess
to start the program you want, storing the return value. You may want to put the external process name in a version block, as the same program might not be available on other operating systems.Use the returned handles to communicate with the process as though it was a file.
Close the handles when you are finished.
Wait for the process to exit.
The code is as follows:
import std.process, std.stdio; auto info = pipeProcess("child_program"); scope(exit) wait(info.pid); info.stdin.writeln("data to send to the process"); info.stdin.close(); foreach(line...