Permalink
Browse files

Removed IO::Pipe and non-blocking read for compatibility

  • Loading branch information...
1 parent c76c0ed commit 107994b27ebf6ea5a9643b1aa856e2aa0a37264b @exodist committed Jul 22, 2010
Showing with 38 additions and 22 deletions.
  1. +0 −1 Build.PL
  2. +34 −17 lib/Child.pm
  3. +4 −4 t/Child.t
View
@@ -9,7 +9,6 @@ my $build = Module::Build->new(
dist_author => 'Chad Granum <exodist7@gmail.com>',
create_readme => 1,
requires => {
- 'IO::Pipe' => 0,
'POSIX' => 0,
},
build_requires => {
View
@@ -141,11 +141,11 @@ sub kill {
sub _gen_ipc {
my $class = shift;
- # Only load if used;
- require IO::Pipe;
+ pipe( my ( $ain, $aout ));
+ pipe( my ( $bin, $bout ));
return [
- IO::Pipe->new,
- IO::Pipe->new,
+ [ $ain, $aout ],
+ [ $bin, $bout ],
];
}
@@ -158,10 +158,9 @@ sub _init_ipc {
$self->_ipc->[0],
]);
}
- $self->_read_handle->reader;
- $self->_read_handle->autoflush(1);
- $self->_write_handle->writer;
- $self->_write_handle->autoflush(1);
+ $self->_ipc->[0] = $self->_ipc->[0]->[0];
+ $self->_ipc->[1] = $self->_ipc->[1]->[1];
+ $self->autoflush(1);
}
sub _read_handle {
@@ -185,11 +184,32 @@ sub _no_pipe {
);
}
+sub autoflush {
+ my $self = shift;
+ my ( $value ) = @_;
+ my $write = $self->_write_handle;
+
+ my $selected = select( $write );
+ $| = ($value || undef) if @_;
+ my $out = $|;
+
+ select( $selected );
+
+ return $out;
+}
+
+sub flush {
+ my $self = shift;
+ my $orig = $self->autoflush();
+ $self->autoflush(1);
+ my $write = $self->_write_handle;
+ print $write "";
+ $self->autoflush($orig);
+}
+
sub read {
my $self = shift;
- my ( $block ) = @_;
my $handle = $self->_read_handle;
- $handle->blocking( $block ? 1 : 0 );
return <$handle>;
}
@@ -244,9 +264,7 @@ waiting, killing, checking, and even communicating with a child process.
}, pipe => 1 );
# Read (blocking)
- my $message1 = $child2->read(1);
-
- # Read (non-blocking)
+ my $message1 = $child2->read();
my $message2 = $child2->read();
$child2->say("reply");
@@ -285,7 +303,7 @@ How child() behaves regarding IPC is lexical to each importing class.
$self->say("message1");
};
- my $message1 = $child->read(1);
+ my $message1 = $child->read();
=head1 CLASS METHODS
@@ -338,10 +356,9 @@ Wait on the child (blocking)
Send the $SIG signal to the child process.
-=item $child->read($BLOCK)
+=item $child->read()
-Read a message from the child. Takes a single boolean argument; when true the
-method blocks.
+Read a message from the child.
=item $child->write( @MESSAGES )
View
@@ -25,21 +25,21 @@ $one = $CLASS->new( sub {
my $self = shift;
$self->say( "Have self" );
$self->say( "parent: " . $self->parent );
- my $in = $self->read(1);
+ my $in = $self->read();
$self->say( $in );
}, pipe => 1 );
$one->start;
-is( $one->read(1), "Have self\n", "child has self" );
-is( $one->read(1), "parent: $$\n", "child has parent PID" );
+is( $one->read(), "Have self\n", "child has self" );
+is( $one->read(), "parent: $$\n", "child has parent PID" );
{
local $SIG{ALRM} = sub { die "non-blocking timeout" };
alarm 5;
ok( !$one->is_complete, "Not Complete" );
alarm 0;
}
$one->say("XXX");
-is( $one->read(1), "XXX\n", "Full IPC" );
+is( $one->read(), "XXX\n", "Full IPC" );
ok( $one->wait, "wait" );
ok( $one->is_complete, "Complete" );
is( $one->exit_status, 0, "Exit clean" );

0 comments on commit 107994b

Please sign in to comment.