perl code to automate launching a program and entering responses

B

Bennett Haselton

If I have a program called foo that does something like:

Hello world. Should I continue? (y/n)
[user must type 'y' and hit Enter before continuing]
Thanks!

then how would I write a perl script that launches foo, waits until it
sees the line "Hello world. Should I continue? (y/n)", and
automatically enters the "y" response, all while printing to stdout
the output that foo is printing and the responses that the perl script
is entering? Actually, how can I do it without downloading and
installing any additional perl modules that don't come with the
standard perl distribution? (Even if there is a specialized class
that does it more elegantly, can I just do it with normal reads and
writes?)

I know this must be simple, and I tried to figure it out myself, but I
couldn't find any phrase to Google for that would give me the answer
(e.g. "perl automate entering input", etc.)

Is it just two lines of code? I know asking people to write code for
you is frowned upon, but in this case wouldn't it be faster than
typing a response in English sentences? :)
 
J

Jürgen Exner

Bennett Haselton said:
If I have a program called foo that does something like:

Hello world. Should I continue? (y/n)
[user must type 'y' and hit Enter before continuing]
Thanks!

then how would I write a perl script that launches foo, waits until it
sees the line "Hello world. Should I continue? (y/n)", and
automatically enters the "y" response, all while printing to stdout
the output that foo is printing and the responses that the perl script
is entering?

You are looking for the Expect module.
Actually, how can I do it without downloading and
installing any additional perl modules that don't come with the
standard perl distribution?

By re-implementing the code from Expect.pm
(Even if there is a specialized class
that does it more elegantly, can I just do it with normal reads and
writes?)

I know this must be simple, and I tried to figure it out myself, but I
couldn't find any phrase to Google for that would give me the answer
(e.g. "perl automate entering input", etc.)

Is it just two lines of code?

Basically you would have to redirect the input and output of the foo
program to be controlled by your program. Only if foo reads/writes
directly from the terminal/to the monitor instead of from/to
stdin/stdout, then it becomes difficult.

jue
 
J

Justin C

If I have a program called foo that does something like:

Hello world. Should I continue? (y/n)
[user must type 'y' and hit Enter before continuing]
Thanks!

then how would I write a perl script that launches foo, waits until it
sees the line "Hello world. Should I continue? (y/n)", and
automatically enters the "y" response, all while printing to stdout
the output that foo is printing and the responses that the perl script
is entering? Actually, how can I do it without downloading and
installing any additional perl modules that don't come with the
standard perl distribution? (Even if there is a specialized class
that does it more elegantly, can I just do it with normal reads and
writes?)

I know this must be simple, and I tried to figure it out myself, but I
couldn't find any phrase to Google for that would give me the answer
(e.g. "perl automate entering input", etc.)

Is it just two lines of code? I know asking people to write code for
you is frowned upon, but in this case wouldn't it be faster than
typing a response in English sentences? :)


Is there a reason you want to do this with perl? If you are on *nix OS
you may find that you have the expect program already.

Justin.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top