kennethk has asked for the wisdom of the Perl Monks concerning the following question:
I get the output#!/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 ); }
This means that when I do the redirect, it mucks up the capture for some reason. If I change sub test toLine 1 This is the first line Line 2 This is the second line Line 3 This is the third line
I get the expectedsub test_mod { my $line = shift; my $content = `echo '$line'`; local $\; print STDOUT $content; }
and as well if I swap sub single_thread toSTDOUT: Line 1 This is the first line STDOUT: Line 2 This is the second line STDOUT: Line 3 This is the third line
I getsub 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"; } }
Any suggestions, other than don't do that? As the subroutine name implies, this is ultimately intended for a multithreaded environment. Essentially, I would like to wrap a subroutine that runs an external command so that it transparently supports threads while maintaining contiguity between each thread's output. I've had this working with backticks for a while, but now I have to modify it to separate the streams and I just don't understand why a core module would show such strange behavior.STDOUT: Line 1 This is the first line STDOUT: Line 2 This is the second line STDOUT: Line 3 This is the third line
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: STDOUT redirects and IPC::Open3
by Eliya (Vicar) on Oct 18, 2011 at 15:26 UTC | |
by kennethk (Abbot) on Oct 18, 2011 at 16:23 UTC | |
by salva (Canon) on Oct 19, 2011 at 16:53 UTC | |
by Eliya (Vicar) on Oct 19, 2011 at 22:33 UTC | |
by salva (Canon) on Oct 20, 2011 at 08:19 UTC | |
by kennethk (Abbot) on Oct 19, 2011 at 15:13 UTC | |
by Eliya (Vicar) on Oct 19, 2011 at 15:36 UTC | |
|
Re: STDOUT redirects and IPC::Open3
by ikegami (Patriarch) on Oct 18, 2011 at 06:31 UTC | |
by kennethk (Abbot) on Oct 18, 2011 at 14:47 UTC |