duc has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I have some problems with bidirectional communication with an .exe application using perl.

Here is what I am trying to do :
I have a c++ console application which asks for a list of values un the standard input, multiply all the elements of the list by a value and print the new list on the standard output. (This is only an example to get me started actually)

Then, I am trying to run the application from a perl script, write to the application some values using the script, and read the new values returned on the standard output from the application.

Here is my perl code for now :

#!/usr/bin/perl use IPC::Open2; my $toExec = "testfigure.exe"; my @xList = (1,2,3,4,5); my $ret; local(*WR, *RD); my $pid = open2(\*RD, \*WR, $toExec); my $length = @xList; print WR "$length\n"; foreach my $xVal (@xList){ print WR "$xVal\n"; } print WR "3\n"; while($ret = <RD>){ print; } waitpid($pid,0);

And here is the problem I'm running into :
I cannot read from the application. The part while($ret = <RD>) does not work, I'm always getting an empty $ret (no reading is done). Though, I can write to the application and it does print @xList multiply by 3 on the screen. I have looked all the examples I could find on the web, and read all the perlipc manpage but there is nothing about such a problem. I am using Windows 2000 professional (maybe it is a known bug).

If anyone has any idea why this code does not work properly, I would really appreciate the help !

Thanks to you

2006-07-23 Retitled by g0n, as per Monastery guidelines
Original title: 'Bidirectionnal Communication with Another Process'

Replies are listed 'Best First'.
Re: Bidirectional Communication with Another Process
by shmem (Chancellor) on Jul 21, 2006 at 18:09 UTC
    Though, I can write to the application and it does print @xList multiply by 3 on the screen.
    Could it be that the application writes to STDERR? Just an idea, I don't have windows. Perhaps it's worth to try IPC::Open3.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
Re: Bidirectional Communication with Another Process
by Ieronim (Friar) on Jul 21, 2006 at 21:40 UTC
    Create a file named "test1" with content
    1 Hello, world! 3
    and execute a command in console:  type test1 | testfigure.exe 2>test2. If the file "test2" contains three "Hello, World!" lines after this call, then shmem was right and your c++ program uses stderr for output. If he was right, IPC::Open3 is your best friend :)

         s;;Just-me-not-h-Ni-m-P-Ni-lm-I-ar-O-Ni;;tr?IerONim-?HAcker ?d;print

      Thank you very much for the help.

      I have tried what you suggested but unfortunately it seems it is not my problem, the file test2 is empty. And I have already tried open3 to get the same result than open2... Maybe it is something with windows, I have read some stuffs about it. I think I will try with a com file, maybe it will work. I'll let you know.

      I have looked at the program and processes section but I guess I will give it a closer look. :)

Re: Bidirectional Communication with Another Process
by planetscape (Chancellor) on Jul 22, 2006 at 02:30 UTC

      I have a "way" to do bidirectional communication with an application using something like :
      @ret = `echo 'cin args' | 'program'`;
      It is not working for me because the list of values I have to pass to my exe application is too long (i.e 241 float values) but it works great for reasonably long list (241 int values).

      Maybe it can help someone...