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 <bodg@tiuk.ti.com>
96 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
97 software; you can redistribute it and/or modify it under the same terms
104 use vars qw($VERSION);
113 my $class = ref($type) || $type || "IO::Pipe";
114 @_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
116 my $me = bless gensym(), $class;
118 my($readfh,$writefh) = @_ ? @_ : $me->handles;
120 pipe($readfh, $writefh)
123 @{*$me} = ($readfh, $writefh);
129 @_ == 1 or croak 'usage: $pipe->handles()';
130 (IO::Handle->new(), IO::Handle->new());
142 elsif(defined $pid) { # Child
143 my $fh = $rw ? $me->reader() : $me->writer();
144 my $io = $rw ? \*STDIN : \*STDOUT;
146 bless $io, "IO::Handle";
147 $io->fdopen($fh, $rw ? "r" : "w");
149 croak "IO::Pipe: Cannot exec: $!";
152 croak "IO::Pipe: Cannot fork: $!";
159 @_ >= 1 or croak 'usage: $pipe->reader()';
162 my $pid = $me->_doit(0,@_)
167 *{*$me} = *{*$fh}; # Alias self to handle
168 bless $fh, 'IO::Pipe::DeadEnd'; # Really wan't un-bless here
169 ${*$me}{'io_pipe_pid'} = $pid
176 @_ >= 1 or croak 'usage: $pipe->writer()';
179 my $pid = $me->_doit(1,@_)
184 *{*$me} = *{*$fh}; # Alias self to handle
185 bless $fh, 'IO::Pipe::DeadEnd'; # Really wan't un-bless here
186 ${*$me}{'io_pipe_pid'} = $pid