Controlling Input and Output of Another Program

Problem

You want to both write to and read from another program. The open function lets you do one or the other, but not both.

Solution

Use the standard IPC::Open2 module:

use IPC::Open2;

open2(*README, *WRITEME, $program);
print WRITEME "here's your input\n";
$output = <README>;
close(WRITEME);
close(README);

Discussion

Wanting simultaneous read and write access to another program is very common, but surprisingly perilous. That’s why open doesn’t let you say:

open(DOUBLE_HANDLE, "| program args |")     # WRONG

The big problem here is buffering. Because you can’t force the other program to unbuffer its output, you can’t guarantee that your reads won’t block. If you block trying to read at the same time the other process blocks waiting for you to send something, you’ve achieved the unholy state of deadlock. There you’ll both stay, wedged, until someone kills your process or the machine reboots.

If you control the other process’s buffering because you wrote the other program and know how it works, then IPC::Open2 may be the module for you. The first two arguments to open2 that IPC::Open2 exports into your namespace are filehandles. Either pass references to typeglobs as in the Solution, or create your own IO::Handle objects and pass them in:

use IPC::Open2;
use IO::Handle;

($reader, $writer) = (IO::Handle->new, IO::Handle->new);
open2($reader, $writer, $program);

If you pass in objects, you must have created them (with IO::Handle->new, for instance) ...

Get Perl Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.