3 # Copyright (c) 1996 Graham Barr <Graham.Barr@tiuk.ti.com>. All rights
4 # reserved. 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);
70 $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
73 $io->fdopen($save, $mode);
74 $save->close or croak "Cannot close $!";
75 croak "IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
79 croak "IO::Pipe: Cannot exec: $!";
83 croak "IO::Pipe: Cannot fork: $!";
90 @_ >= 1 or croak 'usage: $pipe->reader()';
93 my $pid = $me->_doit(0, $fh, @_)
98 *{*$me} = *{*$fh}; # Alias self to handle
99 bless $fh; # Really wan't un-bless here
100 ${*$me}{'io_pipe_pid'} = $pid
107 @_ >= 1 or croak 'usage: $pipe->writer()';
110 my $pid = $me->_doit(1, $fh, @_)
115 *{*$me} = *{*$fh}; # Alias self to handle
116 bless $fh; # Really wan't un-bless here
117 ${*$me}{'io_pipe_pid'} = $pid
123 package IO::Pipe::End;
127 @ISA = qw(IO::Handle);
131 my $r = $fh->SUPER::close(@_);
133 waitpid(${*$fh}{'io_pipe_pid'},0)
134 if(defined ${*$fh}{'io_pipe_pid'});
145 IO::pipe - supply object methods for pipes
151 $pipe = new IO::Pipe;
153 if($pid = fork()) { # Parent
161 elsif(defined $pid) { # Child
169 $pipe = new IO::Pipe;
171 $pipe->reader(qw(ls -l));
179 C<IO::Pipe> provides an interface to createing pipes between
186 =item new ( [READER, WRITER] )
188 Creates a C<IO::Pipe>, which is a reference to a
189 newly created symbol (see the C<Symbol> package). C<IO::Pipe::new>
190 optionally takes two arguments, which should be objects blessed into
191 C<IO::Handle>, or a subclass thereof. These two objects will be used
192 for the system call to C<pipe>. If no arguments are given then then
193 method C<handles> is called on the new C<IO::Pipe> object.
195 These two handles are held in the array part of the GLOB until either
196 C<reader> or C<writer> is called.
204 =item reader ([ARGS])
206 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
207 handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
208 is called and C<ARGS> are passed to exec.
210 =item writer ([ARGS])
212 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
213 handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
214 is called and C<ARGS> are passed to exec.
218 This method is called during construction by C<IO::Pipe::new>
219 on the newly created C<IO::Pipe> object. It returns an array of two objects
220 blessed into C<IO::Pipe::End>, or a subclass thereof.
230 Graham Barr <bodg@tiuk.ti.com>
234 Copyright (c) 1996 Graham Barr. All rights reserved. This program is free
235 software; you can redistribute it and/or modify it under the same terms