home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomePerl CookbookSearch this book

16.8. Controlling Input and Output of Another Program


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


Use the standard IPC::Open2 module:

use IPC::Open2;

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


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) first. The open2 function will not create handles for you if you pass in variables that don't contain filehandles.

Alternatively, you can pass in arguments that look like "<&OTHERFILEHANDLE" or ">&OTHERFILEHANDLE" , which specify existing filehandles for the child process to read from or write to. These filehandles don't have to be controlled by your program - they may be connected to other programs, files, or sockets.

You can specify the program either as a list (where the first element is the program name and the remaining elements are arguments to the program) or as a single string (which is passed to the shell as a command to start the program). If you also want control over the process's standard error, use the IPC::Open3 module and see the next recipe.

If an error occurs, open2 and open3 do not return. Instead, they die with an error message that begins with "open2" or "open3" . To test for failure, use the eval BLOCK construct:

eval {
    open2($readme, $writeme, @program_and_arguments);
if ($@) { 
    if ($@ =~ /^open2/) {
        warn "open2 failed: $!\n$@\n";
    die;            # reraise unforeseen exception

See Also

The documentation for the IPC::Open2 and IPC::Open3 modules; Recipe 10.12 ; the eval function in Chapter 3 of Programming Perl and in perlfunc (1); the $@ variable in the section on "Global Special Variables" in Chapter 2 of Programming Perl and in perlvar (1)

Previous: 16.7. Reading STDERR from a Program Perl Cookbook Next: 16.9. Controlling the Input, Output, and Error of Another Program
16.7. Reading STDERR from a Program Book Index 16.9. Controlling the Input, Output, and Error of Another Program

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.