Click here to get back home

interacting with another program that requires input

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
interacting with another program that requires input whcchoi 05-05-2008
Posted by whcchoi on May 5, 2008, 2:37 pm
Please log in for more thread options
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

Posted by Jürgen Exner on May 5, 2008, 2:43 pm
Please log in for more thread options
whcchoi@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

Posted by Jim Gibson on May 5, 2008, 2:56 pm
Please log in for more thread options
In article

> 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

Posted by zentara on May 6, 2008, 7:09 am
Please log in for more thread options
On Mon, 5 May 2008 11:37:52 -0700 (PDT), whcchoi@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

Similar ThreadsPosted
Handling user input and program busy February 5, 2008, 2:24 pm
Interacting With A Terminal App March 6, 2006, 4:58 pm
Running and Feeding Input to Interactive Program in Windows from Perl Script November 26, 2006, 12:30 pm
best practice ... requires August 23, 2007, 9:39 pm
requires explicit package name January 6, 2005, 6:10 pm
form requires two parameters August 14, 2007, 9:48 pm
Newb requires help with javascript submit September 6, 2004, 1:53 pm
counting perl src when have nested requires? January 23, 2005, 5:41 am
Parse website that requires login March 15, 2007, 3:05 pm
Global symbol requires explicit package name November 4, 2004, 9:46 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap