As another data point, I did some tests with my own simple "echo" program. I was able to confirm that the first problem, "hangs with blank output" was caused by output buffering. I was unable to find any way to get an EOF recognized in "echo2.pl" for reasons that I don't understand. closing $writer didn't do it. So this still hangs, just further along in the process. I was hoping to find some way to get "echo2.pl" to exit, but that didn't happen.

The pipe will have a finite size and if you try to cram too much stuff into it via a blocking write, then it can "fillup" and you can deadlock while trying to cram even more into the pipe. But that's not the problem in this simple example.

I think BrowserUk's suggestion is a good one. Although it is not completely clear to me why this fails. And of course, it is seldom that we have control over buffering settings the other process.

use strict; use warnings; use IPC::Open2; $|=1; $SIG{PIPE} = sub { print STDERR "SIGPIPE received\n"; exit; }; my $pid = open2(my $reader, my $writer, "perl echo2.pl"); print "pid=$pid\n"; print $writer "$_\n" for (qw(first second third )); print "Data written\n"; print $writer chr 4; #try manually send EOF - didn't work! close $writer; #reader still "hangs" print "Reading from pipe:\n"; while( my $in=<$reader>) { print "Received: ".$in."\n"; } close $reader; print "No more data\n"; waitpid($pid, 0); print "Finished\n"; =echo2.pl #!/usr/bin/perl -w use strict; $|++; print "first test line\n"; while (<STDIN>){print;} =cut __END__ C:\TEMP>perl ipc_open2.pl pid=5688 first second third Data written Reading from pipe: Received: first test line Received: first Received: second Received: third ####now hung up in reader ### #### for some reason it doesn't see that input pipe #### closed
Update: Things are a lot easier if this is like a "one line in", "one line out" interface. Or if the response has a "end of transmission" flag, something like blank line means "end of transmission" so that we can quit trying to read more lines that will not be forthcoming.
use strict; use warnings; use IPC::Open2; $|=1; # this is for main program # $writer is alread unbuffered by open2 my $pid = open2(my $reader, my $writer, "perl echo2.pl"); print "pid=$pid\n"; for ( qw(first second third) ) { print $writer "$_\n"; # one line in, one line out my $result = <$reader>; print $result; } =heading #### echo2.pl ##### #!/usr/bin/perl -w use strict; $|=1; while (my $in=<STDIN>){print $in;} print STDERR "out of loop\n"; #never gets here! =cut #### echo2.pl ##### __END__ C:\TEMP>perl ipc_open2.pl pid=3244 first second third

In reply to Re: Can't get it working: Bidirectional Pipe on Windows by Marshall
in thread Can't get it working: Bidirectional Pipe on Windows by rovf

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.