interacting with another program that requires input

W

whcchoi

Hi,
I have a perl script which executes a command which will in term
requires input, I have the following program:

my $cmd = "ipa add";
print "Executing -> $cmd\n\n";

my $pid = open(H, "$cmd |") or die("\n$cmd failed\n");

my($stat, $data);
my $str;

while (sysread(H, $data, 1)) {

if (defined ($data)) {
$str .= $data;

if ($str =~ /Please enter View name\(use \'quit\' to
exit\): /) {
===> NEED HELP HERE
}
}
}

I need help with "NEED HELP HERE", if my cmd "ipa add" requires input
of "abc", how can I provide that and interact with "ipa add"??

I tried:

print H "abc\n";
syswrite() --> don't know what argument to use

None worked. However, It can accept input from STDIN (i.e. I can
interact with my script in the screen where I run my script)..

Please help.

Thanks,
Claudia
 
J

Jürgen Exner

I have a perl script which executes a command which will in term
requires input, I have the following program: [...]
I need help with "NEED HELP HERE", if my cmd "ipa add" requires input
of "abc", how can I provide that and interact with "ipa add"??

You are looking for the Expect module.

jue
 
Z

zentara

I need help with "NEED HELP HERE", if my cmd "ipa add" requires input
of "abc", how can I provide that and interact with "ipa add"??

For bi-directional interaction you need IPC::Open3 , IPC::Open2,
or IPC::Run. Read "perldoc perlipc".

A rudimentary example:

#!/usr/bin/perl
use warnings;
use strict;
use IPC::Open3;

#interface to "bc" calculator
#my $pid = open3(\*WRITE, \*READ, \*ERROR,"bc");
my $pid = open3(\*WRITE, \*READ,0,"bc");
#if \*ERROR is false, STDERR is sent to STDOUT
while(1){
print "Enter expression for bc, i.e. 2 + 2\n";
chomp(my $query = <STDIN>);

#send query to bc
print WRITE "$query\n";

#give bc time to output
select(undef,undef,undef,.5);

#get the answer from bc
chomp(my $answer = <READ>);
print "$query = $answer\n";

}

waitpid($pid, 1);
# It is important to waitpid on your child process,
# otherwise zombies could be created.
__END__

#############################################

You can also add IO::Select to be fancier.

#!/usr/bin/perl

# Something like this:
# It's only drawback is it only outputs 1 line of bc output
# so it errs on something like 234^12345 (which outputs a big number)

use warnings;
use strict;
use IPC::Open3;
use IO::Select;

#interface to "bc" calculator
my $pid = open3(\*WRITE, \*READ,\*ERROR,"bc");

my $sel = new IO::Select();

$sel->add(\*READ);
$sel->add(\*ERROR);

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";

foreach my $h ($sel->can_read)
{
my $buf = '';
if ($h eq \*ERROR)
{
sysread(ERROR,$buf,4096);
if($buf){print "ERROR-> $buf\n"}
}
else
{
sysread(READ,$buf,4096);
if($buf){print "$query = $buf\n"}
}
}
}

waitpid($pid, 1);

__END__


zentara
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,743
Messages
2,569,478
Members
44,899
Latest member
RodneyMcAu

Latest Threads

Top