
Zissou
New User
Oct 29, 2008, 6:08 AM
Post #8 of 8
(2088 views)
|
Re: [Zissou] Automatically Answering Application Questions Using Perl
[In reply to]
|
Can't Post
|
|
For those who are interest I have a solution that does not involve Expect. I used the example posted here http://perldoc.perl.org/perlipc.html#Bidirectional-Communication-with-Another-Process as template. open2(<reader?, <writer>, <cmd>) was the key once I figured out the I needed to declare the reader and writer before I used them in the open2 function. I had to use getc because by just accessing the reader the program was hanging when it did not receive \n Here is the code: $pid = open2($Reader, $Writer, "./<program or cmd to run>"); $Writer->autoflush(1); $question = ""; while ($got = getc $Reader){ print "got a char: $got\n"; $question .= "$got"; if ($got eq "\n"){ $question = ""; } if ($question eq "Some question 1 ") { print "found what i was looking for\n"; print $Writer "Answer to question 1\n"; } elsif ($question eq "Some question 2 " ) { print $Writer "Answer to question 2\n"; } elsif ($question eq "Some question 3 ") { print $Writer "Answer to question 3\n"; last; } } close Reader; close Writer;Best Regards, Zissou
(This post was edited by Zissou on Oct 29, 2008, 6:14 AM)
|