karden has asked for the wisdom of the Perl Monks concerning the following question:
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).
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.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
However such a code block would perfectly work (again there is a read followed by a write):
I desperately need your opinion.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";
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: can't read before closing the writer in open2
by jasonk (Parson) on Jul 10, 2007 at 14:43 UTC | |
|
Re: can't read before closing the writer in open2
by zentara (Cardinal) on Jul 10, 2007 at 15:31 UTC | |
|
Re: can't read before closing the writer in open2
by karden (Novice) on Jul 10, 2007 at 17:05 UTC |