8 IPC::Open3, open3 - open a process for reading, writing, and error handling
12 $pid = open3(\*WTRFH, \*RDRFH, \*ERRFH
13 'some cmd and args', 'optarg', ...);
17 Extremely similar to open2(), open3() spawns the given $cmd and
18 connects RDRFH for reading, WTRFH for writing, and ERRFH for errors. If
19 ERRFH is '', or the same as RDRFH, then STDOUT and STDERR of the child are
20 on the same file handle.
22 If WTRFH begins with "<&", then WTRFH will be closed in the parent, and
23 the child will read from it directly. If RDRFH or ERRFH begins with
24 ">&", then the child will send output directly to that file handle. In both
25 cases, there will be a dup(2) instead of a pipe(2) made.
27 If you try to read from the child's stdout writer and their stderr
28 writer, you'll have problems with blocking, which means you'll
29 want to use select(), which means you'll have to use sysread() instead
32 All caveats from open2() continue to apply. See L<open2> for details.
39 # &open3: Marc Horowitz <marc@mit.edu>
40 # derived mostly from &open2 by tom christiansen, <tchrist@convex.com>
41 # fixed for 5.001 by Ulrich Kunitz <kunitz@mai-koeln.com>
43 # $Id: open3.pl,v 1.1 1993/11/23 06:26:15 marc Exp $
45 # usage: $pid = open3('wtr', 'rdr', 'err' 'some cmd and args', 'optarg', ...);
47 # spawn the given $cmd and connect rdr for
48 # reading, wtr for writing, and err for errors.
49 # if err is '', or the same as rdr, then stdout and
50 # stderr of the child are on the same fh. returns pid
51 # of child, or 0 on failure.
54 # if wtr begins with '<&', then wtr will be closed in the parent, and
55 # the child will read from it directly. if rdr or err begins with
56 # '>&', then the child will send output directly to that fd. In both
57 # cases, there will be a dup() instead of a pipe() made.
60 # WARNING: this is dangerous, as you may block forever
61 # unless you are very careful.
63 # $wtr is left unbuffered.
67 # pipe or fork or exec fails
69 $fh = 'FHOPEN000'; # package static in case called more than once
73 my($dad_wtr, $dad_rdr, $dad_err, @cmd) = @_;
74 my($dup_wtr, $dup_rdr, $dup_err);
76 $dad_wtr || croak "open3: wtr should not be null";
77 $dad_rdr || croak "open3: rdr should not be null";
78 $dad_err = $dad_rdr if ($dad_err eq '');
80 $dup_wtr = ($dad_wtr =~ s/^[<>]&//);
81 $dup_rdr = ($dad_rdr =~ s/^[<>]&//);
82 $dup_err = ($dad_err =~ s/^[<>]&//);
84 # force unqualified filehandles into callers' package
85 my($package) = caller;
86 $dad_wtr =~ s/^[^:]+$/$package\:\:$&/ unless ref $dad_wtr;
87 $dad_rdr =~ s/^[^:]+$/$package\:\:$&/ unless ref $dad_rdr;
88 $dad_err =~ s/^[^:]+$/$package\:\:$&/ unless ref $dad_err;
95 pipe($kid_rdr, $dad_wtr) || croak "open3: pipe 1 (stdin) failed: $!";
98 pipe($dad_rdr, $kid_wtr) || croak "open3: pipe 2 (stdout) failed: $!";
100 if ($dad_err ne $dad_rdr && !$dup_err) {
101 pipe($dad_err, $kid_err) || croak "open3: pipe 3 (stderr) failed: $!";
104 if (($kidpid = fork) < 0) {
105 croak "open3: fork failed: $!";
106 } elsif ($kidpid == 0) {
108 open(STDIN, "<&$dad_wtr") if (fileno(STDIN) != fileno($dad_wtr));
111 open(STDIN, "<&$kid_rdr");
114 open(STDOUT, ">&$dad_rdr") if (fileno(STDOUT) != fileno($dad_rdr));
117 open(STDOUT, ">&$kid_wtr");
119 if ($dad_rdr ne $dad_err) {
121 open(STDERR, ">&$dad_err")
122 if (fileno(STDERR) != fileno($dad_err));
125 open(STDERR, ">&$kid_err");
128 open(STDERR, ">&STDOUT") if (fileno(STDERR) != fileno(STDOUT));
132 or croak "open3: exec of @cmd failed";
135 close $kid_rdr; close $kid_wtr; close $kid_err;
140 select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
143 1; # so require is happy