AnyEvent-Open3-Simple

 view release on metacpan or  search on metacpan

lib/AnyEvent/Open3/Simple.pm  view on Meta::CPAN

  elsif($self->{impl} eq 'idle')
  {
    my($selout, $selerr);

    if(_is_native_win32())
    {
      require IO::Select;
      $selout = IO::Select->new($child_stdout);
      $selerr = IO::Select->new($child_stderr);
    }

    $watcher_child = AnyEvent->idle(cb => sub {
      if(_is_native_win32())
      {
        $stdout_callback->() if $selout->can_read(0);
        $stderr_callback->() if $selerr->can_read(0);
      }
      AnyEvent::Open3::Simple::Idle::_watcher($pid, $end_cb);
    });
  }
  else
  {
    $watcher_child = AnyEvent->child(
      pid => $pid,
      cb  => $end_cb,
    );
  }

  $self;
}

1;

__END__

=pod

=encoding UTF-8

=head1 NAME

AnyEvent::Open3::Simple - Interface to open3 under AnyEvent

=head1 VERSION

version 0.90

=head1 SYNOPSIS

 use 5.010;
 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;
   },
 );
 
 $ipc->run('echo', 'hello there');
 $done->recv;

=head1 DESCRIPTION

This module provides an interface to open3 while running under AnyEvent
that delivers data from stdout and stderr as lines are written by the
subprocess.  The interface is reminiscent of L<IPC::Open3::Simple>,
although this module does provides a somewhat different API, so it
cannot be used a drop in replacement for that module.

There are already a number of interfaces for interacting with subprocesses
in the context of L<AnyEvent>, but this one is the most convenient for my
usage.  Note the modules listed in the SEE ALSO section below for other
interfaces that may be more or less appropriate.

=head1 CONSTRUCTOR

Constructor takes a hash or hashref of event callbacks and attributes.
Event callbacks have an C<on_> prefix, attributes do not.

=head2 ATTRIBUTES

=over 4

=item * implementation

The implementation to use for detecting process termination.  This should
be one of C<child>, C<idle> or C<mojo>.  On all platforms except for Microsoft
Windows (but not Cygwin) the default is C<child>.

You can change the default by setting the C<ANYEVENT_OPEN3_SIMPLE>
environment variable, like this:

 % export ANYEVENT_OPEN3_SIMPLE=idle

The C<mojo> implementation is experimental and allows you to use
L<AnyEvent::Open3::Simple> with L<Mojolicious> but without L<EV>
(which is usually required for L<AnyEvent>, L<Mojolicious> interaction).

=back

=head2 EVENTS

These events will be triggered by the subprocess when the run method is
called. Each event callback (except C<on_error>) gets passed in an
instance of L<AnyEvent::Open3::Simple::Process> as its first argument
which can be used to get the PID of the subprocess, or to write to it.
C<on_error> does not get a process object because it indicates an error in



( run in 0.962 second using v1.01-cache-2.11-cpan-483215c6ad5 )