Hey all!

I am trying to make a bidirectional communication to an external program using IPC::Open2. The problem is: I cannot "read" program's output unless I close the writer handle before attempting to read.

Such a problem is mentioned also here (I am not using Oracle but the essence of the problem is the same i guess): http://rootshell.be/~yong321/computer/OracleAndPerl.html (Pls see the first code block in Appendix section, also copied below).

use IPC::Open2; local (*Reader, *Writer); $pid = open2(\*Reader, \*Writer, "e:/oracle/ora81/bin/sqlplus -s scott +/tiger"); print Writer "set pagesize 100\n"; print Writer "select * from emp;\n"; print Writer "exit\n"; close Writer; #have to close Writer before read #have to read and print one line at a time while (<Reader>) { print "$_"; } close Reader; waitpid($pid, 0); #makes your program cleaner
It says: "the Writer has to be closed before you can read from the Reader". Why? Is there a way to overcome because I need to make read-write-read-write-read-... sequentially and I cannot open a new connection every time I want to read.

However such a code block would perfectly work (again there is a read followed by a write):

use IPC::Open2; local (*Reader, *Writer); $pid = open2(\*Reader, \*Writer, "bc -l"); $sum = 2; for (1 .. 5) { print Writer "$sum * $sum\n"; chomp($sum = <Reader>); } close Writer; close Reader; waitpid($pid, 0); print "sum is $sum\n";
I desperately need your opinion.

In reply to can't read before closing the writer in open2 by karden

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.