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

Hello world. Should I continue? (y/n)
(and here the 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? :)

In reply to perl code to automate launching a program and entering responses by bennetthaselton

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.