7 IO::Pipe - supply object methods for pipes
15 if($pid = fork()) { # Parent
23 elsif(defined $pid) { # Child
33 $pipe->reader(qw(ls -l));
41 C<IO::Pipe> provides an interface to createing pipes between
48 =item new ( [READER, WRITER] )
50 Creates a C<IO::Pipe>, which is a reference to a
51 newly created symbol (see the C<Symbol> package). C<IO::Pipe::new>
52 optionally takes two arguments, which should be objects blessed into
53 C<IO::Handle>, or a subclass thereof. These two objects will be used
54 for the system call to C<pipe>. If no arguments are given then then
55 method C<handles> is called on the new C<IO::Pipe> object.
57 These two handles are held in the array part of the GLOB until either
58 C<reader> or C<writer> is called.
68 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
69 handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
70 is called and C<ARGS> are passed to exec.
74 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
75 handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
76 is called and C<ARGS> are passed to exec.
80 This method is called during construction by C<IO::Pipe::new>
81 on the newly created C<IO::Pipe> object. It returns an array of two objects
82 blessed into C<IO::Handle>, or a subclass thereof.
92 Graham Barr E<lt>F<bodg@tiuk.ti.com>E<gt>
100 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
101 software; you can redistribute it and/or modify it under the same terms
107 use vars qw($VERSION);
112 $VERSION = sprintf("%d.%02d", q$Revision: 1.7 $ =~ /(\d+)\.(\d+)/);
116 my $class = ref($type) || $type || "IO::Pipe";
117 @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
119 my $me = bless gensym(), $class;
121 my($readfh,$writefh) = @_ ? @_ : $me->handles;
123 pipe($readfh, $writefh)
126 @{*$me} = ($readfh, $writefh);
132 @_ == 1 or croak 'usage: $pipe->handles()';
133 (IO::Handle->new(), IO::Handle->new());
145 elsif(defined $pid) { # Child
146 my $fh = $rw ? $me->reader() : $me->writer();
147 my $io = $rw ? \*STDIN : \*STDOUT;
149 bless $io, "IO::Handle";
150 $io->fdopen($fh, $rw ? "r" : "w");
152 croak "IO::Pipe: Cannot exec: $!";
155 croak "IO::Pipe: Cannot fork: $!";
162 @_ >= 1 or croak 'usage: $pipe->reader()';
165 my $pid = $me->_doit(0,@_)
169 *{*$me} = *{*$fh}; # Alias self to handle
170 bless $fh; # Really wan't un-bless here
171 ${*$me}{'io_pipe_pid'} = $pid
178 @_ >= 1 or croak 'usage: $pipe->writer()';
181 my $pid = $me->_doit(1,@_)
185 *{*$me} = *{*$fh}; # Alias self to handle
186 bless $fh; # Really wan't un-bless here
187 ${*$me}{'io_pipe_pid'} = $pid