xeronuro
11/17/2012 - 4:41 PM

Mojo::Lite Ex. using IO::Async

Mojo::Lite Ex. using IO::Async

use Mojolicious::Lite;
use EV;
use IO::Async::Process;
use IO::Async::Loop::EV;

my $loop = IO::Async::Loop::EV->new;

get '/' => sub {
  my $self = shift;

  # Run Perl oneliner in separate process and capture STDOUT
  my $result  = '';
  my $process = IO::Async::Process->new(
    command => ['perl', '-E', "say 'hi!'"],
    stdout  => {
      on_read => sub {
        my ($stream, $buffref) = @_;
        $result .= $1 while $$buffref =~ s/^(.*)\n//;
        return 0;
      },
    },

    on_finish => sub { $self->render(text => "Result: $result") },
  );
  $loop->add($process);
};

app->start;