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::new> creates a C<IO::Pipe>, which is a reference to a
42 newly created symbol (see the C<Symbol> package). C<IO::Pipe::new>
43 optionally takes two arguments, which should be objects blessed into
44 C<IO::Handle>, or a subclass thereof. These two objects will be used
45 for the system call to C<pipe>. If no arguments are given then then
46 method C<handles> is called on the new C<IO::Pipe> object.
48 These two handles are held in the array part of the GLOB untill either
49 C<reader> or C<writer> is called.
53 =item $fh->reader([ARGS])
55 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
56 handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
57 is called and C<ARGS> are passed to exec.
59 =item $fh->writer([ARGS])
61 The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
62 handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
63 is called and C<ARGS> are passed to exec.
67 This method is called during construction by C<IO::Pipe::new>
68 on the newly created C<IO::Pipe> object. It returns an array of two objects
69 blessed into C<IO::Handle>, or a subclass thereof.
79 Graham Barr <bodg@tiuk.ti.com>
87 Copyright (c) 1995 Graham Barr. All rights reserved. This program is free
88 software; you can redistribute it and/or modify it under the same terms
94 use vars qw($VERSION);
99 $VERSION = sprintf("%d.%02d", q$Revision: 1.4 $ =~ /(\d+)\.(\d+)/);
102 @_ == 1 || @_ == 3 or croak 'usage: new IO::Pipe([$READFH, $WRITEFH])';
104 my $me = bless gensym(), shift;
106 my($readfh,$writefh) = @_ ? @_ : $me->handles;
108 pipe($readfh, $writefh)
111 @{*$me} = ($readfh, $writefh);
117 @_ == 1 or croak 'usage: $pipe->handles()';
118 (IO::Handle->new(), IO::Handle->new());
130 elsif(defined $pid) { # Child
131 my $fh = $rw ? $me->reader() : $me->writer();
132 my $io = $rw ? \*STDIN : \*STDOUT;
134 bless $io, "IO::Handle";
135 $io->fdopen($fh, $rw ? "r" : "w");
137 croak "IO::Pipe: Cannot exec: $!";
140 croak "IO::Pipe: Cannot fork: $!";
147 @_ >= 1 or croak 'usage: $pipe->reader()';
150 my $pid = $me->_doit(0,@_)
154 *{*$me} = *{*$fh}; # Alias self to handle
155 ${*$me}{'io_pipe_pid'} = $pid
162 @_ >= 1 or croak 'usage: $pipe->writer()';
165 my $pid = $me->_doit(1,@_)
169 *{*$me} = *{*$fh}; # Alias self to handle
170 ${*$me}{'io_pipe_pid'} = $pid