Perl and DOS I/O

H

Hemant Kumar

I want to start a DOS program from perl, read its output and give it
further input when the program prompts (based on output of the
program).

Suppose the DOS program is as follows

#include <stdio.h>

void main() {
int choice;
srand();

do {
printf ("\n%d\n", rand());
printf ("Do you want to do more ?\n");
choice = getchar();
} while ( choice != 'n');

printf("\nGoodbye\n");
}

I want the perl script to read the results generated. Based on results
read, give input for getchar. I have tried using system(), Open (this
only allows me to do either input or output but not both). I don't
have the source for the DOS program so need to do both input and
output from Perl.
Can you suggest a way to get around these limitations ?

Thanks a lot,
Hemant
 
B

Ben Morrow

Quoth (e-mail address removed) (Hemant Kumar):
I want to start a DOS program from perl, read its output and give it
further input when the program prompts (based on output of the
program).

Suppose the DOS program is as follows

#include <stdio.h>

void main() {
int choice;
srand();

do {
printf ("\n%d\n", rand());
printf ("Do you want to do more ?\n");
choice = getchar();
} while ( choice != 'n');

printf("\nGoodbye\n");
}

I want the perl script to read the results generated. Based on results
read, give input for getchar. I have tried using system(), Open (this
only allows me to do either input or output but not both). I don't
have the source for the DOS program so need to do both input and
output from Perl.
Can you suggest a way to get around these limitations ?

The standard answer is IPC::Open2, but I don't know how well this will
work under Win32. You may want to try Win32::process instead.

IME, getting DOS programs to behave nicely is Very Difficult: it is
usually easier to arrange things so you can just do

system "dosprog.exe < input > output";

and process the results: it looks like this isn't possible in your case,
though.
 
G

Graham Wood

Hemant said:
I want to start a DOS program from perl, read its output and give it
further input when the program prompts (based on output of the
program).
<snip>

The module IPC::Open2 gives you connections to the input and output of a
process. Alternatively you could try Expect.pm. Expect designed to
interact with command line programs, to expect certain output from the
programs and to provide appropriate responses to that output. There is
also an IPC::Open3 which gives you STDERR as well as STDIN and STDOUT.

You get IPC::Open2 in perl 5.6.1 but you have to install Expect from CPAN.

I haven't tried any of these.

perldoc IPC::Open2 for details.

Graham
 
J

Jürgen Exner

Hemant said:
I want to start a DOS program from perl, read its output and give it
further input when the program prompts (based on output of the
program).

perldoc -q expect

jue
 
R

Richard Gration

I want the perl script to read the results generated. Based on results
read, give input for getchar. I have tried using system(), Open (this
only allows me to do either input or output but not both). I don't have
the source for the DOS program so need to do both input and output from
Perl.
Can you suggest a way to get around these limitations ? Thanks a lot,
Hemant

I'd echo the comments of Graham Wood, use IPC::Open2. I used it to drive
a coin toss program which was in essence identical to your random num
generator. It's very easy, if you adapt the example from the docs of
IPC::Open2 you should have a working program in about 5 or 10 minutes!

Rich
 
H

Hemant Kumar

Thank you for the response. I used Open2 and faced issues with output
buffering on the C side. My program hangs (I believe waiting for the
input which will never come since its waiting for the output to give
that precious character)

It works well when I explicitly flush buffers in the example using
fflush (alas I don't have the source for the C program that I really
need to use). Next stop Expect package.

Thanks,
Hemant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top