AnyEvent-Open3-Simple
view release on metacpan or search on metacpan
use AnyEvent;
use AnyEvent::Open3::Simple;
my $done = AnyEvent->condvar;
my $ipc = AnyEvent::Open3::Simple->new(
on_start => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $program = shift; # string
my @args = @_; # list of arguments
say 'child PID: ', $proc->pid;
},
on_stdout => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $line = shift; # string
say 'out: ', $string;
},
on_stderr => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $line = shift; # string
say 'err: ', $line;
},
on_exit => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $exit_value = shift; # integer
my $signal = shift; # integer
say 'exit value: ', $exit_value;
say 'signal: ', $signal;
$done->send;
},
on_error => sub {
my $error = shift; # the exception thrown by IPC::Open3::open3
my $program = shift; # string
my @args = @_; # list of arguments
warn "error: $error";
$done->send;
},
);
hold the native Windows port of Perl in high regard: problems such as
this may pop up again in the future and may not be addressed, and may
be out of the control of the author of this module.
Performance for the idle watcher implementation on native Windows
(non-Cygwin) is almost certainly suboptimal, but the author of this
module uses it and finds it useful despite this.
Writing to a subprocesses stdin with
AnyEvent::Open3::Simple::Process#print or
AnyEvent::Open3::Simple::Process#say is unsupported on Microsoft
Windows (it does work under Cygwin though).
There are some traps for the unwary relating to buffers and deadlocks,
IPC::Open3 is recommended reading.
If you register a call back for on_exit, but not on_error then use a
condition variable to wait for the process to complete as in this:
my $cv = AnyEvent->condvar;
my $ipc = AnyEvent::Open3::Simple->new(
lib/AnyEvent/Open3/Simple.pm view on Meta::CPAN
use AnyEvent;
use AnyEvent::Open3::Simple;
my $done = AnyEvent->condvar;
my $ipc = AnyEvent::Open3::Simple->new(
on_start => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $program = shift; # string
my @args = @_; # list of arguments
say 'child PID: ', $proc->pid;
},
on_stdout => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $line = shift; # string
say 'out: ', $string;
},
on_stderr => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $line = shift; # string
say 'err: ', $line;
},
on_exit => sub {
my $proc = shift; # isa AnyEvent::Open3::Simple::Process
my $exit_value = shift; # integer
my $signal = shift; # integer
say 'exit value: ', $exit_value;
say 'signal: ', $signal;
$done->send;
},
on_error => sub {
my $error = shift; # the exception thrown by IPC::Open3::open3
my $program = shift; # string
my @args = @_; # list of arguments
warn "error: $error";
$done->send;
},
);
lib/AnyEvent/Open3/Simple/Process.pm view on Meta::CPAN
}
sub pid { shift->{pid} }
if($^O eq 'MSWin32')
{
require Carp;
*print = sub { Carp::croak("AnyEvent::Open3::Simple::Process#print is unsupported on this platform") };
*say = sub { Carp::croak("AnyEvent::Open3::Simple::Process#say is unsupported on this platform") };
}
else
{
*print = sub {
my $stdin = shift->{stdin};
print $stdin @_;
};
*say = sub {
my $stdin = shift->{stdin};
print $stdin @_, "\n";
};
}
sub close
{
CORE::close(shift->{stdin});
}
lib/AnyEvent/Open3/Simple/Process.pm view on Meta::CPAN
Do NOT use this method if you have passed stdin via the C<$stdin> argument
on the L<AnyEvent::Open3::Simple#run> method.
Currently on (non cygwin) Windows (Strawberry, ActiveState) this method is not
supported, so if you need to send (standard) input to the subprocess, you must pass
it into the L<AnyEvent::Open3::Simple#run> method.
=head2 say
$proc->say(@data);
Write to the subprocess' stdin, adding a new line at the end.
Do NOT use this method if you have passed stdin via the C<$stdin> argument
on the L<AnyEvent::Open3::Simple#run> method.
Currently on (non cygwin) Windows (Strawberry, ActiveState) this method is not
supported, so if you need to send (standard) input to the subprocess, you must pass
it into the L<AnyEvent::Open3::Simple#run> method.
lib/AnyEvent/Open3/Simple/Process.pm view on Meta::CPAN
between callbacks, for example:
AnyEvent::Open3::Simple->new(
on_start => sub {
my($proc) = @_;
$proc->user({ prefix => '> ' });
},
on_stdout => sub {
my($proc, $line) = @_;
my $prefix = $proc->user->{prefix};
say "$prefix$line";
},
);
=head1 SEE ALSO
=over 4
=item L<AnyEvent::Open3::Simple>
=back
t/anyevent_open3_simple__print.t view on Meta::CPAN
close $fh;
my $done = AnyEvent->condvar;
my $ipc = AnyEvent::Open3::Simple->new(
on_exit => sub {
$done->send;
},
on_start => sub {
my($proc) = @_;
eval { $proc->say('message1') };
diag $@ if $@;
eval { $proc->say('message2') };
diag $@ if $@;
eval { $proc->close };
diag $@ if $@;
},
);
my $timeout = AnyEvent->timer(
after => 5,
cb => sub { diag 'timeout!'; exit 2 },
);
( run in 0.338 second using v1.01-cache-2.11-cpan-483215c6ad5 )