Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello

First of all let me state my problem. I have a program that I execute via a system call, and it prompts the user to hit any key to continue.

What I want to do is automate this step for them. I've searched the archives, and found several posts but unfortunatley they didn't work (i.e.

system ( "echo y | command_I'm_running" );

Does anyone have any ideas?

Replies are listed 'Best First'.
Re: forcing STDIN with windows
by Mr. Muskrat (Canon) on Aug 06, 2003 at 17:19 UTC

    You could also use Win32::GuiTest.

    use Win32::GuiTest qw(FindWindowLike SetForegroundWindow SendKeys); system("start p.bat"); # start ensures that the progra +m opens in a new window sleep(1); # give p.bat a chance to load my @win = FindWindowLike(0, "^p.bat"); # find p.bat's window (maybe mo +re than one) SetForegroundWindow($win[0]); # bring the first p.bat window +to the foreground SendKeys("y"); # send a keypress to the active + window in this case a "y" __DATA__ For this example, p.bat contains one line: pause

Re: forcing STDIN with windows
by BrowserUk (Patriarch) on Aug 06, 2003 at 21:16 UTC

    If the program doesn't accept the input from a pipe, it probably means that it is using direct access to the console or even simply flushing the input buffer prior to prompting.

    As it's a console program, I'm not sure if Win32:CtrlGUI will succeed in feeding it input in the right way. If not, then you could also try using Win32::Console and the WriteInput(event) call. Be sure to use the STD_INPUT_HANDLE parameter for the new() call to get a handle to the existing console input buffer rather than creating a new one.

    Even this may not work. For instance, if I was righting a program to accept a password from the console and I wanted to ensure that a human being was entering the password rather than a ghost writing program, I might create a new input console just for the purposes of retrieving that password without the FILE_SHARE_WRITE permission and discard it afterwards. In this case, there should be no way for any other program to 'feed' the input. Hopefully, a yes or no answer to a prompt won't have required such precautions:)

    Good luck.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller
    If I understand your problem, I can solve it! Of course, the same can be said for you.

Re: forcing STDIN with windows
by Aristotle (Chancellor) on Aug 06, 2003 at 17:00 UTC
    Are you on a Unixoid OS?
    system ( "yes | foo" );
    Update: hm, it says Windows in the title..

    Makeshifts last the longest.

Re: forcing STDIN with windows
by Limbic~Region (Chancellor) on Aug 06, 2003 at 16:52 UTC
    Anonymous Monk,
    Since I haven't used it, I can't give you working code, but I suspect what you want is Expect.

    Cheers - L~R

    Update: It appears that Win32 is only supported through Cygwin.

Re: forcing STDIN with windows
by Mr. Muskrat (Canon) on Aug 06, 2003 at 17:05 UTC

    Perhaps the program you are executing is looking for a specific "any" key ;-) because this works for me...

    system("echo y | pause"); __DATA__ C:\WINDOWS\Desktop>perl echo.pl Press any key to continue . . . C:\WINDOWS\Desktop>