Skip to content

Instantly share code, notes, and snippets.

@arodland
Created April 20, 2016 02:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arodland/bf5de98dfb4503583fb1e51ae3aa0c67 to your computer and use it in GitHub Desktop.
Save arodland/bf5de98dfb4503583fb1e51ae3aa0c67 to your computer and use it in GitHub Desktop.
#!/usr/bin/perl
use strict;
use warnings;
use IO::Async;
use IO::Async::Loop;
use IO::Async::Process;
my $cmd1 = [qw(ls -1)];
my $cmd2 = [qw(rev)];
my $transform = sub {
return uc $_[0];
};
my $loop = IO::Async::Loop->new;
my ($process1, $process2);
$process1 = IO::Async::Process->new(
command => $cmd1,
stdout => {
on_read => sub {
my ($self, $buffref) = @_;
while ($$buffref =~ s/^(.*?\n)//) {
$process2->stdin->write(
$transform->($1)
);
}
},
},
on_finish => sub {
my ($self, $exit) = @_;
warn "process1 exited with code $exit\n";
$process2->stdin->close;
}
);
$process2 = IO::Async::Process->new(
command => $cmd2,
stdin => {
via => 'pipe_write',
},
stdout => {
on_read => sub {
my ($self, $buffref) = @_;
while ($$buffref =~ s/^(.*?\n)//) {
print "Output: $1";
}
},
},
on_finish => sub {
my ($self, $exit) = @_;
warn "process2 exited with code $exit\n";
$loop->stop;
}
);
$loop->add($process1);
$loop->add($process2);
$loop->run;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment