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

Hi,

I want to automate the calling of an executable (on win32) that asks for the user to type in some input. If I ran it through the command prompt, it would look like:

> TheExe.exe
> Please enter your age: (user types age here, then return)
> Please enter your height: (..)

Is it possible to make a script that calls the executable then just feeds it all the input answers one after another? I want to automate it because it's growing tiresome having to type all the answers everytime I run the exe manually.

I don't have the source of the executable I'm wanting to run so I can't make a batch run option for it, sadly.

Thanks

Replies are listed 'Best First'.
Re: Call external exe
by roboticus (Chancellor) on Aug 13, 2008 at 16:43 UTC
    markww:

    Read perlipc and look at the section named "Using open() for IPC" for an example on how to do that. You may also want to try Super Search for other discussions on the subject.

    ...roboticus
Re: Call external exe
by zentara (Cardinal) on Aug 13, 2008 at 17:23 UTC
    To respond to ikegami and Bloodnok at the same time, I guess Ikegami is right, no Expect on Win32 (unless Cygwin). Also IPC::Open3 will not work on Win32 reliably, although on linux it would be the ideal solution. See Perl/Tk App and Interprocess Communication The problem is with select on the READ filehandle. You may be able to use IPC::Open3 if you know for sure what the prompt will be from the exe, and you can just feed it your data with a little time delay in between.
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; my ($age, $height, $weight) = (50,72,250); #yeah I'm fat :-) #my $pid = open3(\*WRITE, \*READ, \*ERROR, 'somecmd.exe'); my $pid = open3(\*WRITE, \*READ,0,'somecmd.exe'); #if \*ERROR is false, STDERR is sent to STDOUT #send query to cmd print WRITE "$age\n"; #give cmd time to output select(undef,undef,undef,.5); print WRITE "$height\n"; select(undef,undef,undef,.5); print WRITE "$weight\n"; select(undef,undef,undef,.5); #get the answer from cmd if needed chomp(my $answer = <READ>); print "$answer\n"; waitpid($pid, 1);

    I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Call external exe
by Bloodnok (Vicar) on Aug 13, 2008 at 16:54 UTC
    Hi markww,

    Have a look @ Section 16 of the Perl Cookbook - there are recipes in there that use IPC::Open3 to control input/output to/from another program.

    HTH ,

    A user level that continues to overstate my experience :-))
Re: Call external exe
by zentara (Cardinal) on Aug 13, 2008 at 16:49 UTC
    You probably want to consider moving up to a gui, like Tk, to get input and feed it to your program, or you could use Expect. Read "perldoc Expect", it does what you want.

    I'm not really a human, but I play one on earth Remember How Lucky You Are

      or you could use Expect. Read "perldoc Expect", it does what you want.

      I thought Expect didn't work on Windows (except possible in Cygwin)?

      You probably want to consider moving up to a gui, like Tk, to get input and feed it to your program

      Nowhere does he say he needs input form the user. In fact, it sounds like he wants to save the user from having to input constant data.