From: whcchoi on
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
From: J�rgen Exner on
whcchoi(a)gmail.com wrote:
>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
From: Jim Gibson on
In article
<cffdc6bd-a5be-4353-83ce-d26a2c5478dc(a)m45g2000hsb.googlegroups.com>,
<whcchoi(a)gmail.com> wrote:

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

Open with a pipe ("$cmd |") only affords unidirectional communication.
For bidirectional I/O, see 'perldoc perlipc' and search for
'Bidirectional'. The advice there is to use open2 or, better, the
Expect.pm module from CPAN.

--
Jim Gibson

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
http://www.usenet.com
From: zentara on
On Mon, 5 May 2008 11:37:52 -0700 (PDT), whcchoi(a)gmail.com wrote:

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

--
I'm not really a human, but I play one on earth.
http://zentara.net/japh.html