3 # Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
4 # This program is free software; you can redistribute it and/or
5 # modify it under the same terms as Perl itself.
13 use vars qw($VERSION);
21 my $class = ref($type) || $type || "IO::Pipe";
22 @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
24 my $me = bless gensym(), $class;
26 my($readfh,$writefh) = @_ ? @_ : $me->handles;
28 pipe($readfh, $writefh)
31 @{*$me} = ($readfh, $writefh);
37 @_ == 1 or croak 'usage: $pipe->handles()';
38 (IO::Pipe::End->new(), IO::Pipe::End->new());
41 my $do_spawn = $^O eq 'os2';
47 my $pid = $do_spawn ? 0 : fork();
52 elsif(defined $pid) { # Child or spawn
54 my $io = $rw ? \*STDIN : \*STDOUT;
55 my ($mode, $save) = $rw ? "r" : "w";
58 $save = IO::Handle->new_from_fd($io, $mode);
60 fcntl(shift, Fcntl::F_SETFD(), 1) or croak "fcntl: $!";
61 $fh = $rw ? ${*$me}[0] : ${*$me}[1];
64 $fh = $rw ? $me->reader() : $me->writer(); # close the other end
66 bless $io, "IO::Handle";
67 $io->fdopen($fh, $mode);
71 $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
74 $io->fdopen($save, $mode);
75 $save->close or croak "Cannot close $!";
76 croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
80 croak "IO::Pipe: Cannot exec: $!";
84 croak "IO::Pipe: Cannot fork: $!";
91 @_ >= 1 or croak 'usage: $pipe->reader( [SUB_COMMAND_ARGS] )';
95 unless(ref($me) || ref($me = $me->new));
98 my $pid = $me->_doit(0, $fh, @_)
103 *{*$me} = *{*$fh}; # Alias self to handle
104 $me->fdopen($fh->fileno,"r")
105 unless defined($me->fileno);
106 bless $fh; # Really wan't un-bless here
107 ${*$me}{'io_pipe_pid'} = $pid
114 @_ >= 1 or croak 'usage: $pipe->writer( [SUB_COMMAND_ARGS] )';
118 unless(ref($me) || ref($me = $me->new));
121 my $pid = $me->_doit(1, $fh, @_)
126 *{*$me} = *{*$fh}; # Alias self to handle
127 $me->fdopen($fh->fileno,"w")
128 unless defined($me->fileno);
129 bless $fh; # Really wan't un-bless here
130 ${*$me}{'io_pipe_pid'} = $pid
136 package IO::Pipe::End;
140 @ISA = qw(IO::Handle);
144 my $r = $fh->SUPER::close(@_);
146 waitpid(${*$fh}{'io_pipe_pid'},0)
147 if(defined ${*$fh}{'io_pipe_pid'});
158 IO::Pipe - supply object methods for pipes
164 $pipe = new IO::Pipe;
166 if($pid = fork()) { # Parent
174 elsif(defined $pid) { # Child
182 $pipe = new IO::Pipe;
184 $pipe->reader(qw(ls -l));
192 C<IO::Pipe> provides an interface to createing pipes between
199 =item new ( [READER, WRITER] )
201 Creates a C<IO::Pipe>, which is a reference to a newly created symbol
202 (see the C<Symbol> package). C<IO::Pipe::new> optionally takes two
203 arguments, which should be objects blessed into C<IO::Handle>, or a
204 subclass thereof. These two objects will be used for the system call
205 to C<pipe>. If no arguments are given then method C<handles> is called
206 on the new C<IO::Pipe> object.
208 These two handles are held in the array part of the GLOB until either
209 C<reader> or C<writer> is called.
217 =item reader ([ARGS])
219 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
220 handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
221 is called and C<ARGS> are passed to exec.
223 =item writer ([ARGS])
225 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
226 handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
227 is called and C<ARGS> are passed to exec.
231 This method is called during construction by C<IO::Pipe::new>
232 on the newly created C<IO::Pipe> object. It returns an array of two objects
233 blessed into C<IO::Pipe::End>, or a subclass thereof.
243 Graham Barr <gbarr@pobox.com>
247 Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
248 This program is free software; you can redistribute it and/or
249 modify it under the same terms as Perl itself.