Skip to content

Instantly share code, notes, and snippets.

@plaster
Last active August 29, 2015 14:05
Show Gist options
  • Save plaster/a4e9afd000c170d64aa7 to your computer and use it in GitHub Desktop.
Save plaster/a4e9afd000c170d64aa7 to your computer and use it in GitHub Desktop.
use utf8;
use strict;
use IO::File;
use IPC::Open2;
my $ifh0 = IO::Handle->new;
my $ifh1 = IO::File->new_tmpfile;
my $ofh0 = IO::Handle->new;
my $pid0 = open2($ifh0, $ofh0, qw(cat -n));
my $pid1 = open2('>&' . $ifh1->fileno, '<&' . $ifh0->fileno, qw(tac));
for (my $i = 1; $i < 20; ++$i) {
print $ofh0 ('x' x $i), "\n";
}
$ofh0->close;
$ifh0->close;
print STDERR "written to process\n";
waitpid($pid0, 0);
waitpid($pid1, 0);
$ifh1->seek(0, 0);
while (my $line = <$ifh1>) {
printf STDERR "read: %s", $line;
}
exit(0);
% perl pipetest.pl
written to process
read: 19 xxxxxxxxxxxxxxxxxxx
read: 18 xxxxxxxxxxxxxxxxxx
read: 17 xxxxxxxxxxxxxxxxx
read: 16 xxxxxxxxxxxxxxxx
read: 15 xxxxxxxxxxxxxxx
read: 14 xxxxxxxxxxxxxx
read: 13 xxxxxxxxxxxxx
read: 12 xxxxxxxxxxxx
read: 11 xxxxxxxxxxx
read: 10 xxxxxxxxxx
read: 9 xxxxxxxxx
read: 8 xxxxxxxx
read: 7 xxxxxxx
read: 6 xxxxxx
read: 5 xxxxx
read: 4 xxxx
read: 3 xxx
read: 2 xx
read: 1 x
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment