#!/usr/bin/perl -w
use strict;
use IPC::Open3;
use Symbol 'gensym';
my @queue = ("Line 1\nThis is the first line\n",
"Line 2\nThis is the second line\n",
"Line 3\nThis is the third line\n",
);
single_thread(@queue);
sub single_thread {
open my $oldout, ">&", \*STDOUT or die "Can't dup STDOUT: $!";
for (@_) {
local *STDOUT;
open STDOUT, '>', \my $stdout or die "Can't redirect STDOUT: $!";
test($_);
print $oldout "STDOUT: $stdout\n" if defined $stdout;
close STDOUT;
}
}
sub test {
my $line = shift;
my $pid = open3(my $wtr, my $rdr, undef, 'cat', '-v');
print $wtr $line;
close $wtr;
my $content=do{local $/;<$rdr>};
local $\;
print STDOUT $content;
waitpid( $pid, 0 );
}
####
Line 1
This is the first line
Line 2
This is the second line
Line 3
This is the third line
####
sub test_mod {
my $line = shift;
my $content = `echo '$line'`;
local $\;
print STDOUT $content;
}
####
STDOUT: Line 1
This is the first line
STDOUT: Line 2
This is the second line
STDOUT: Line 3
This is the third line
####
sub single_thread {
for my $line (@_) {
my $pid = open3(my $wtr, my $rdr, undef, 'cat', '-v');
print $wtr $line;
close $wtr;
my $content=do{local $/;<$rdr>};
local $\;
print "STDOUT: $content\n";
}
}
####
STDOUT: Line 1
This is the first line
STDOUT: Line 2
This is the second line
STDOUT: Line 3
This is the third line