Hi guys, I've been given a command line program that is an executable (.exe) that I was asked to automate on Win32. The task was simple; First enter a number, Enter a string, than 'Press any key to continue...' (dos system call it seems). And the program will reply successful if those values were valid. I decided to use IPC::Open2. The code was as follows..
use strict; use warnings; use IPC::Open2; my $cmd = 'C:\temp\abc.exe'; my $pid = open2(*RR,*WW,$cmd); print WW "5 \n"; print WW "iAmString \n"; print WW "\n"; # any key while (<RR>) { chomp($_); print "$_\n"; } print "done \n";
But the code did not work at all - it froze on the <RR> block. So I went ahead a compiled a simple C code to simulate the problem.
int main(int argc, char** argv) { printf ("Hello World \n"); int value1; printf ("I am a number: "); scanf("%d", &value1); printf ("This is your number: %d \n",value1); char ppp[20]; printf ("I am a string: " ); scanf("%s",&ppp); printf ("This is your string? %10.10s \n",ppp); // system("PAUSE"); return (EXIT_SUCCESS); }
And, the perl script was stuck at the blocking <RR> as expected. I changed the C Program and removed the 'system("pause") - press any key to continue' command, and it worked brilliantly. It appears to me that after the main part of the C program is executed it is blocked by 'cmd.exe /c PAUSE' that I believe is spawned as a blocking child process. i.e i can't write "<any key>" to the process I created through open2. Rather I might have to send <any key> to the cmd.exe that was spawned as a child of abc.exe. Is there a workaround for this? (I don't have access to the source code of the executable, so i can't simply remove the system("PAUSE"). Help is a appreciated.

In reply to Perl Open2/3 and .exe by markusk

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.