Pipe.pm revision 7c478bd95313f5f23a4c958a745db2134aa03244
# IO::Pipe.pm
#
# Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
# modify it under the same terms as Perl itself.
require 5.005_64;
use strict;
our($VERSION);
use Carp;
use Symbol;
$VERSION = "1.121";
sub new {
my $type = shift;
@_ == 0 || @_ == 2 or croak "usage: new $class [READFH, WRITEFH]";
or return undef;
$me;
}
sub handles {
@_ == 1 or croak 'usage: $pipe->handles()';
}
my $do_spawn = $^O eq 'os2';
sub _doit {
my $me = shift;
my $rw = shift;
if($pid) { # Parent
return $pid;
}
elsif(defined $pid) { # Child or spawn
my $fh;
if ($do_spawn) {
require Fcntl;
# Close in child:
} else {
shift;
}
bless $io, "IO::Handle";
$fh->close;
if ($do_spawn) {
my $err = $!;
$save->close or croak "Cannot close $!";
return $pid;
} else {
exec @_ or
croak "IO::Pipe: Cannot exec: $!";
}
}
else {
croak "IO::Pipe: Cannot fork: $!";
}
# NOT Reached
}
sub reader {
@_ >= 1 or croak 'usage: $pipe->reader( [SUB_COMMAND_ARGS] )';
my $me = shift;
return undef
if(@_);
close ${*$me}[1];
unless defined($me->fileno);
bless $fh; # Really wan't un-bless here
if defined $pid;
$me;
}
sub writer {
@_ >= 1 or croak 'usage: $pipe->writer( [SUB_COMMAND_ARGS] )';
my $me = shift;
return undef
if(@_);
close ${*$me}[0];
unless defined($me->fileno);
bless $fh; # Really wan't un-bless here
if defined $pid;
$me;
}
our(@ISA);
sub close {
my $fh = shift;
if(defined ${*$fh}{'io_pipe_pid'});
$r;
}
1;
=head1 NAME
IO::Pipe - supply object methods for pipes
=head1 SYNOPSIS
use IO::Pipe;
$pipe = new IO::Pipe;
if($pid = fork()) { # Parent
$pipe->reader();
while(<$pipe> {
....
}
}
elsif(defined $pid) { # Child
$pipe->writer();
print $pipe ....
}
or
$pipe = new IO::Pipe;
$pipe->reader(qw(ls -l));
while(<$pipe>) {
....
}
=head1 DESCRIPTION
C<IO::Pipe> provides an interface to creating pipes between
processes.
=head1 CONSTRUCTOR
=over 4
=item new ( [READER, WRITER] )
Creates a 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.
=back
=head1 METHODS
=over 4
=item reader ([ARGS])
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.
=item writer ([ARGS])
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.
=item handles ()
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.
=back
=head1 SEE ALSO
L<IO::Handle>
=head1 AUTHOR
Graham Barr. Currently maintained by the Perl Porters. Please report all
bugs to <perl5-porters@perl.org>.
=head1 COPYRIGHT
Copyright (c) 1996-8 Graham Barr <gbarr@pobox.com>. All rights reserved.
modify it under the same terms as Perl itself.
=cut