#!/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 ); }