Update...
This works great in Linux but now I'm trying to get it working in Windows. The problem is that Windows doesn't seem to have the process print to the pipe that's attached to stdout. The only thing I can gather from testing variations is that STDOUT is the same for both "processes" in Windows. If I sleep and let the child run first or don't restore STDOUT, the pipe works as designed. As soon as I restore STDOUT, the pipe is broken even though they are separate processes.
Is there any way of doing this in Windows?
print "before the pipe\n";
open $old_stdout, ">&STDOUT" or die "open: $!";
local(*WH);
pipe $rh, WH;
open STDOUT, ">&WH" or die "open: $!";
STDOUT->blocking(0);
# write something to the pipe
# and have some fork/exec'ed process write to the pipe (via stdout)
if( fork() ) {
open( STDOUT, ">&", $old_stdout ) or die "open: $!";
print STDERR "I'm the parent\n";
sleep 3;
} else {
exec( 'echo fubar' );
exit( 0 );
}
close WH;
my $out = <$rh>;
print "got from pipe: $out"; # $out is empty since fubar printed to
+the screen
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.
|
|