Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Automating user input

by Nishchita (Initiate)
on Feb 06, 2013 at 12:30 UTC ( #1017404=perlquestion: print w/ replies, xml ) Need Help??
Nishchita has asked for the wisdom of the Perl Monks concerning the following question:

Hi Perl monks. Ok, so, I have a situation. There is a script which needs user input. & I need to automate running of this script. Which means I cannot provide the user inputs as <STDIN>. Expect module is not an option for me as the scripts need to run on Windows. Any suggestions? Here is a sample code. my $count = 0; while ($count < 2) { $count ++; print "Question? y/n: "; chomp(my $answer = <>); print "You said `$answer`\n"; }

I am not allowed to edit the above script. Now I need to automate running the above script. And my answer needs to be "y" followed by "n" I tried `echo y|perl` This takes care of One user input as "y" . I don't know how to feed in the second input "n" Any leads will be much appreciated !

Comment on Automating user input
Download Code
Replies are listed 'Best First'.
Re: Automating user input
by BrowserUk (Pope) on Feb 06, 2013 at 12:38 UTC

    C:\test>copy con answers.txt y n ^Z 1 file(s) copied. C:\test>type my $count = 0; while ($count < 2) { $count ++; print "Question? y/n: "; chomp(my $answer = <>); print "You said `$answer`\n"; } C:\test>type answers.txt | perl Question? y/n: You said `y` Question? y/n: You said `n` C:\test>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Automating user input
by kennethk (Abbot) on Feb 06, 2013 at 15:45 UTC
    If you need to be actually interactive rather than just stream inputs, there are a number of solutions discussed in perlipc. IPC::Open2 is my guess on your best bet in that case.

    #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Automating user input
by Plankton (Priest) on Feb 06, 2013 at 21:10 UTC
Re: Automating user input
by sam_bakki (Pilgrim) on Feb 07, 2013 at 06:23 UTC


    You can try Win32::GuiTest and using FindWindowLike , SendKeys APIs, you can send key strokes to Any window (including cmd prompt)

    I have done a script some time back to do the similar job.

    1. start the app / script which needs an input in background and redirect the output to file, (Ex:start perl.exe > out.txt 2>&1 )
    2. Monitor out.txt in your automate script and look for patterns to enter your input
    3. use Win32::GuiTest and send keystroke to the app/script which was started in background.

    Thanks & Regards,
    Bakkiaraj M
    My Perl Gtk2 technology demo project - , contributions are welcome.

Re: Automating user input
by Nishchita (Initiate) on Feb 10, 2013 at 08:37 UTC

    Wow, thanks! The type method suggested by BrowserUk is what I have implemented as of now. I will look at the other solutions provided & update y'all!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1017404]
Front-paged by Arunbear
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2015-11-28 03:40 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (737 votes), past polls