Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > perl code to automate launching a program and entering responses

Reply
Thread Tools

perl code to automate launching a program and entering responses

 
 
Bennett Haselton
Guest
Posts: n/a
 
      12-11-2010
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?
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      12-11-2010
Bennett Haselton <> wrote:
>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
 
Reply With Quote
 
 
 
 
Justin C
Guest
Posts: n/a
 
      12-13-2010
On 2010-12-11, Bennett Haselton <> wrote:
> 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.

--
Justin C, by the sea.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
stop program with entering a key... hazal Ates Ruby 8 07-28-2010 11:43 AM
Program to automate saving of website pages richard Computer Support 12 09-04-2006 11:26 PM
Automate program execution problem (Runtime.exec("java" + " Hello.java"); seenuvas Java 7 01-24-2006 07:04 PM
Automate program execution problem (Runtime.exec("java" + " Hello.java"); seenuvas Java 0 01-24-2006 01:01 PM
Program entering the default case of switch statement always Phoe6 C Programming 12 02-28-2005 12:30 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57