[inseparable changes from patch from perl5.003_24 to perl5.003_25]
[p5sagit/p5-mst-13.2.git] / ext / IO / lib / IO / Pipe.pm
CommitLineData
774d564b 1# IO::Pipe.pm
8add82fc 2#
774d564b 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.
8add82fc 6
7package IO::Pipe;
8
774d564b 9require 5.000;
10
11use IO::Handle;
12use strict;
13use vars qw($VERSION);
14use Carp;
15use Symbol;
16
17$VERSION = "1.09";
18
19sub new {
20 my $type = shift;
21 my $class = ref($type) || $type || "IO::Pipe";
22 @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
23
24 my $me = bless gensym(), $class;
25
26 my($readfh,$writefh) = @_ ? @_ : $me->handles;
27
28 pipe($readfh, $writefh)
29 or return undef;
30
31 @{*$me} = ($readfh, $writefh);
32
33 $me;
34}
35
36sub handles {
37 @_ == 1 or croak 'usage: $pipe->handles()';
38 (IO::Pipe::End->new(), IO::Pipe::End->new());
39}
40
41my $do_spawn = $^O eq 'os2';
42
43sub _doit {
44 my $me = shift;
45 my $rw = shift;
46
47 my $pid = $do_spawn ? 0 : fork();
48
49 if($pid) { # Parent
50 return $pid;
51 }
52 elsif(defined $pid) { # Child or spawn
53 my $fh;
54 my $io = $rw ? \*STDIN : \*STDOUT;
55 my ($mode, $save) = $rw ? "r" : "w";
56 if ($do_spawn) {
57 require Fcntl;
58 $save = IO::Handle->new_from_fd($io, $mode);
59 # Close in child:
60 fcntl(shift, Fcntl::F_SETFD(), 1) or croak "fcntl: $!";
61 $fh = $rw ? ${*$me}[0] : ${*$me}[1];
62 } else {
63 shift;
64 $fh = $rw ? $me->reader() : $me->writer(); # close the other end
65 }
66 bless $io, "IO::Handle";
67 $io->fdopen($fh, $mode);
68
69 if ($do_spawn) {
70 $pid = eval { system 1, @_ }; # 1 == P_NOWAIT
71 my $err = $!;
72
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;
76 return $pid;
77 } else {
78 exec @_ or
79 croak "IO::Pipe: Cannot exec: $!";
80 }
81 }
82 else {
83 croak "IO::Pipe: Cannot fork: $!";
84 }
85
86 # NOT Reached
87}
88
89sub reader {
90 @_ >= 1 or croak 'usage: $pipe->reader()';
91 my $me = shift;
92 my $fh = ${*$me}[0];
93 my $pid = $me->_doit(0, $fh, @_)
94 if(@_);
95
96 close ${*$me}[1];
97 bless $me, ref($fh);
98 *{*$me} = *{*$fh}; # Alias self to handle
99 bless $fh; # Really wan't un-bless here
100 ${*$me}{'io_pipe_pid'} = $pid
101 if defined $pid;
102
103 $me;
104}
105
106sub writer {
107 @_ >= 1 or croak 'usage: $pipe->writer()';
108 my $me = shift;
109 my $fh = ${*$me}[1];
110 my $pid = $me->_doit(1, $fh, @_)
111 if(@_);
112
113 close ${*$me}[0];
114 bless $me, ref($fh);
115 *{*$me} = *{*$fh}; # Alias self to handle
116 bless $fh; # Really wan't un-bless here
117 ${*$me}{'io_pipe_pid'} = $pid
118 if defined $pid;
119
120 $me;
121}
122
123package IO::Pipe::End;
124
125use vars qw(@ISA);
126
127@ISA = qw(IO::Handle);
128
129sub close {
130 my $fh = shift;
131 my $r = $fh->SUPER::close(@_);
132
133 waitpid(${*$fh}{'io_pipe_pid'},0)
134 if(defined ${*$fh}{'io_pipe_pid'});
135
136 $r;
137}
138
1391;
140
141__END__
142
8add82fc 143=head1 NAME
144
7a4c00b4 145IO::pipe - supply object methods for pipes
8add82fc 146
147=head1 SYNOPSIS
148
149 use IO::Pipe;
150
151 $pipe = new IO::Pipe;
152
153 if($pid = fork()) { # Parent
154 $pipe->reader();
155
156 while(<$pipe> {
157 ....
158 }
159
160 }
161 elsif(defined $pid) { # Child
162 $pipe->writer();
163
164 print $pipe ....
165 }
166
167 or
168
169 $pipe = new IO::Pipe;
170
171 $pipe->reader(qw(ls -l));
172
173 while(<$pipe>) {
174 ....
175 }
176
177=head1 DESCRIPTION
178
27d4819a 179C<IO::Pipe> provides an interface to createing pipes between
180processes.
181
182=head1 CONSTRCUTOR
183
184=over 4
185
186=item new ( [READER, WRITER] )
187
188Creates a C<IO::Pipe>, which is a reference to a
8add82fc 189newly created symbol (see the C<Symbol> package). C<IO::Pipe::new>
190optionally takes two arguments, which should be objects blessed into
191C<IO::Handle>, or a subclass thereof. These two objects will be used
192for the system call to C<pipe>. If no arguments are given then then
193method C<handles> is called on the new C<IO::Pipe> object.
194
27d4819a 195These two handles are held in the array part of the GLOB until either
8add82fc 196C<reader> or C<writer> is called.
197
27d4819a 198=back
199
200=head1 METHODS
201
202=over 4
8add82fc 203
27d4819a 204=item reader ([ARGS])
8add82fc 205
206The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
207handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
208is called and C<ARGS> are passed to exec.
209
27d4819a 210=item writer ([ARGS])
8add82fc 211
212The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
213handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
214is called and C<ARGS> are passed to exec.
215
27d4819a 216=item handles ()
8add82fc 217
218This method is called during construction by C<IO::Pipe::new>
219on the newly created C<IO::Pipe> object. It returns an array of two objects
774d564b 220blessed into C<IO::Pipe::End>, or a subclass thereof.
8add82fc 221
222=back
223
224=head1 SEE ALSO
225
226L<IO::Handle>
227
228=head1 AUTHOR
229
7a4c00b4 230Graham Barr <bodg@tiuk.ti.com>
8add82fc 231
232=head1 COPYRIGHT
233
774d564b 234Copyright (c) 1996 Graham Barr. All rights reserved. This program is free
8add82fc 235software; you can redistribute it and/or modify it under the same terms
236as Perl itself.
237
238=cut