AnyEvent-Subprocess
view release on metacpan or search on metacpan
lib/AnyEvent/Subprocess.pm view on Meta::CPAN
package AnyEvent::Subprocess;
BEGIN {
$AnyEvent::Subprocess::VERSION = '1.102912';
}
# ABSTRACT: flexible, OO, asynchronous process spawning and management
use Moose;
with 'AnyEvent::Subprocess::Job';
our $VERSION;
use AnyEvent::Subprocess::DefaultDelegates;
use namespace::autoclean;
__PACKAGE__->meta->make_immutable;
1;
=pod
=head1 NAME
AnyEvent::Subprocess - flexible, OO, asynchronous process spawning and management
=head1 VERSION
version 1.102912
=head1 SYNOPSIS
use AnyEvent::Subprocess;
# prepare the job
my $job = AnyEvent::Subprocess->new(
delegates => ['StandardHandles'],
on_completion => sub { die 'bad exit status' unless $_[0]->is_success },
code => sub {
my %args = %{$_[0]};
while(<>){
print "Got line: $_";
}
exit 0;
},
);
# start the child
my $run = $job->run;
# add watcher to print the next line we see on the child's stdout
$run->delegate('stdout')->handle->push_read( line => sub {
my ($h, $line) = @_;
say "The child said: $line";
});
# write to the child's stdin
$run->delegate('stdin')->handle->push_write("Hello, world!\n");
# close stdin after it has been written to the child
$run->delegate('stdin')->handle->on_drain(sub { $_[0]->close_fh });
# kill the child if it takes too long to produce a result
my $killer = AnyEvent->timer( after => 42, interval => 0, cb => sub {
$run->kill(2); # SIGINT.
});
# ensure the event loop runs until the on_completion handler dies
EV::loop(); # you can use any AnyEvent-compatible event loop, including POE
# eventually prints "The child said: Got line: Hello, world!", or
# perhaps dies if your system is really really overloaded.
( run in 0.673 second using v1.01-cache-2.11-cpan-39bf76dae61 )