# Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
# This program is free software; you can redistribute it and/or
# modify it under the same terms as Perl itself.
my $class = ref($type) || $type || "IO::Pipe";
@_ == 0 || @_ == 2 or croak
"usage: new $class [READFH, WRITEFH]";
my $me = bless gensym
(), $class;
my($readfh,$writefh) = @_ ?
@_ : $me->handles;
@
{*$me} = ($readfh, $writefh);
@_ == 1 or croak
'usage: $pipe->handles()';
(IO
::Pipe
::End
->new(), IO
::Pipe
::End
->new());
my $do_spawn = $^O
eq 'os2' || $^O
eq 'MSWin32';
my $pid = $do_spawn ?
0 : fork();
elsif(defined $pid) { # Child or spawn
my $io = $rw ? \
*STDIN
: \
*STDOUT
;
my ($mode, $save) = $rw ?
"r" : "w";
$save = IO
::Handle
->new_from_fd($io, $mode);
unless ($^O
eq 'MSWin32') {
fcntl($handle, Fcntl
::F_SETFD
(), 1) or croak
"fcntl: $!";
$fh = $rw ?
${*$me}[0] : ${*$me}[1];
$fh = $rw ?
$me->reader() : $me->writer(); # close the other end
$pid = eval { system 1, @_ }; # 1 == P_NOWAIT
$io->fdopen($save, $mode);
$save->close or croak
"Cannot close $!";
croak
"IO::Pipe: Cannot spawn-NOWAIT: $err" if not $pid or $pid < 0;
croak
"IO::Pipe: Cannot exec: $!";
croak
"IO::Pipe: Cannot fork: $!";
@_ >= 1 or croak
'usage: $pipe->reader( [SUB_COMMAND_ARGS] )';
unless(ref($me) || ref($me = $me->new));
$pid = $me->_doit(0, $fh, @_)
*$me = *$fh; # Alias self to handle
$me->fdopen($fh->fileno,"r")
unless defined($me->fileno);
bless $fh; # Really wan't un-bless here
${*$me}{'io_pipe_pid'} = $pid
@_ >= 1 or croak
'usage: $pipe->writer( [SUB_COMMAND_ARGS] )';
unless(ref($me) || ref($me = $me->new));
$pid = $me->_doit(1, $fh, @_)
*$me = *$fh; # Alias self to handle
$me->fdopen($fh->fileno,"w")
unless defined($me->fileno);
bless $fh; # Really wan't un-bless here
${*$me}{'io_pipe_pid'} = $pid
my $r = $fh->SUPER::close(@_);
waitpid(${*$fh}{'io_pipe_pid'},0)
if(defined ${*$fh}{'io_pipe_pid'});
IO::Pipe - supply object methods for pipes
if($pid = fork()) { # Parent
elsif(defined $pid) { # Child
$pipe->reader(qw(ls -l));
C<IO::Pipe> provides an interface to creating pipes between
=item new ( [READER, WRITER] )
Creates an C<IO::Pipe>, which is a reference to a newly created symbol
(see the C<Symbol> package). C<IO::Pipe::new> optionally takes two
arguments, which should be objects blessed into C<IO::Handle>, or a
subclass thereof. These two objects will be used for the system call
to C<pipe>. If no arguments are given then method C<handles> is called
on the new C<IO::Pipe> object.
These two handles are held in the array part of the GLOB until either
C<reader> or C<writer> is called.
The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
handle at the reading end of the pipe. If C<ARGS> are given then C<fork>
is called and C<ARGS> are passed to exec.
The object is re-blessed into a sub-class of C<IO::Handle>, and becomes a
handle at the writing end of the pipe. If C<ARGS> are given then C<fork>
is called and C<ARGS> are passed to exec.
This method is called during construction by C<IO::Pipe::new>
on the newly created C<IO::Pipe> object. It returns an array of two objects
blessed into C<IO::Pipe::End>, or a subclass thereof.
Graham Barr. Currently maintained by the Perl Porters. Please report all
bugs to <perl5-porters@perl.org>.
Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.