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 All caveats from open2() continue to apply. See L<open2> for details.
34 # &open3: Marc Horowitz <marc@mit.edu>
35 # derived mostly from &open2 by tom christiansen, <tchrist@convex.com>
36 # fixed for 5.001 by Ulrich Kunitz <kunitz@mai-koeln.com>
38 # $Id: open3.pl,v 1.1 1993/11/23 06:26:15 marc Exp $
40 # usage: $pid = open3('wtr', 'rdr', 'err' 'some cmd and args', 'optarg', ...);
42 # spawn the given $cmd and connect rdr for
43 # reading, wtr for writing, and err for errors.
44 # if err is '', or the same as rdr, then stdout and
45 # stderr of the child are on the same fh. returns pid
46 # of child, or 0 on failure.
49 # if wtr begins with '<&', then wtr will be closed in the parent, and
50 # the child will read from it directly. if rdr or err begins with
51 # '>&', then the child will send output directly to that fd. In both
52 # cases, there will be a dup() instead of a pipe() made.
55 # WARNING: this is dangerous, as you may block forever
56 # unless you are very careful.
58 # $wtr is left unbuffered.
62 # pipe or fork or exec fails
64 $fh = 'FHOPEN000'; # package static in case called more than once
68 my($dad_wtr, $dad_rdr, $dad_err, @cmd) = @_;
69 my($dup_wtr, $dup_rdr, $dup_err);
71 $dad_wtr || croak "open3: wtr should not be null";
72 $dad_rdr || croak "open3: rdr should not be null";
73 $dad_err = $dad_rdr if ($dad_err eq '');
75 $dup_wtr = ($dad_wtr =~ s/^[<>]&//);
76 $dup_rdr = ($dad_rdr =~ s/^[<>]&//);
77 $dup_err = ($dad_err =~ s/^[<>]&//);
79 # force unqualified filehandles into callers' package
80 my($package) = caller;
81 $dad_wtr =~ s/^[^:]+$/$package\:\:$&/;
82 $dad_rdr =~ s/^[^:]+$/$package\:\:$&/;
83 $dad_err =~ s/^[^:]+$/$package\:\:$&/;
90 pipe($kid_rdr, $dad_wtr) || croak "open3: pipe 1 (stdin) failed: $!";
93 pipe($dad_rdr, $kid_wtr) || croak "open3: pipe 2 (stdout) failed: $!";
95 if ($dad_err ne $dad_rdr && !$dup_err) {
96 pipe($dad_err, $kid_err) || croak "open3: pipe 3 (stderr) failed: $!";
99 if (($kidpid = fork) < 0) {
100 croak "open2: fork failed: $!";
101 } elsif ($kidpid == 0) {
103 open(STDIN, "<&$dad_wtr") if (fileno(STDIN) != fileno($dad_wtr));
106 open(STDIN, "<&$kid_rdr");
109 open(STDOUT, ">&$dad_rdr") if (fileno(STDOUT) != fileno($dad_rdr));
112 open(STDOUT, ">&$kid_wtr");
114 if ($dad_rdr ne $dad_err) {
116 open(STDERR, ">&$dad_err")
117 if (fileno(STDERR) != fileno($dad_err));
120 open(STDERR, ">&$kid_err");
123 open(STDERR, ">&STDOUT") if (fileno(STDERR) != fileno(STDOUT));
127 croak "open2: exec of @cmd failed";
130 close $kid_rdr; close $kid_wtr; close $kid_err;
135 select((select($dad_wtr), $| = 1)[0]); # unbuffer pipe
138 1; # so require is happy