Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Controling and interactive program

by jblapham (Novice)
on Aug 13, 2004 at 20:12 UTC ( [id://382824]=perlquestion: print w/replies, xml ) Need Help??

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

Has anyone ever written a perl script to control and interactive program. One that prompts for input. I want to call the program, wait for the prompt, send it the input, and capture the return code. I'm not sure how to do this. Any examples are welcome.

Thanks, Jim

Replies are listed 'Best First'.
Re: Controling and interactive program
by Paladin (Vicar) on Aug 13, 2004 at 20:25 UTC
    If this is a command line program you want to control, try the Expect module.
      Yeah, either Expect or IPC::Run should do it.
Re: Controling and interactive program
by etcshadow (Priest) on Aug 13, 2004 at 22:23 UTC
    IPC::Run... specifically, read the section on pump.
    ------------ :Wq Not an editor command: Wq
Re: Controling and interactive program
by johnnywang (Priest) on Aug 13, 2004 at 20:32 UTC
Re: Controling and interactive program
by zentara (Archbishop) on Aug 14, 2004 at 13:12 UTC
    Here is an example using 'bc' the calculator. It uses IPC::Open3. (There are pitfalls to watch out for with IPC, like when you try to get huge output thru the buffered pipes....but you will know it if you hit that problem :-) )
    #!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; #interface to "bc" calculator my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc"); #if \*ERROR is false, STDERR is sent to STDOUT my $selread = new IO::Select(); my $selerror = new IO::Select(); $selread->add(\*READ); $selerror->add(\*ERROR); # may not be best use of IO::Select, but it works :-) my($error,$answer)=('',''); while(1){ print "Enter expression for bc, i.e. 2 + 2\n"; chomp(my $query = <STDIN>); #send query to bc print WRITE "$query\n"; #timing delay needed tp let bc output select(undef,undef,undef,.01); #see which filehandles have output if($selread->can_read(0)){print "ready->read\n"} if($selerror->can_read(0)){print "ready->error\n"} #get any error from bc sysread(ERROR,$error,4096) if $selerror->can_read(0); if($error){print "\e[1;31m ERROR-> $error \e[0m \n"} #get the answer from bc sysread(READ,$answer,4096) if $selread->can_read(0); if($answer){print "$query = $answer\n"} ($error,$answer)=('',''); } waitpid($pid, 1); # It is important to waitpid on your child process, # otherwise zombies could be created.

    I'm not really a human, but I play one on earth. flash japh

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://382824]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (2)
As of 2024-04-20 09:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found