perl code to automate launching a program and entering responses

Discussion in 'Perl Misc' started by Bennett Haselton, Dec 11, 2010.

  1. 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? :)
     
    Bennett Haselton, Dec 11, 2010
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, Dec 11, 2010
    #2
    1. Advertising

  3. Bennett Haselton

    Justin C Guest

    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.
     
    Justin C, Dec 13, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    4,987
    Hans Kesting
    Jun 21, 2005
  2. Michael
    Replies:
    2
    Views:
    6,253
    Michael
    Nov 9, 2005
  3. Phoe6
    Replies:
    12
    Views:
    609
    Gordon Burditt
    Feb 28, 2005
  4. Colin Howell
    Replies:
    2
    Views:
    574
    John Bokma
    Apr 25, 2010
  5. hazal Ates
    Replies:
    8
    Views:
    141
    hazal Ates
    Jul 28, 2010
Loading...

Share This Page