interacting with another program that requires input

Discussion in 'Perl Misc' started by whcchoi@gmail.com, May 5, 2008.

  1. Guest

    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
     
    , May 5, 2008
    #1
    1. Advertising

  2. 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
     
    Jürgen Exner, May 5, 2008
    #2
    1. Advertising

  3. zentara Guest

    On Mon, 5 May 2008 11:37:52 -0700 (PDT), 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
     
    zentara, May 6, 2008
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Timothy Parez

    Interacting with Windows Services

    Timothy Parez, Apr 7, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    1,833
  2. James
    Replies:
    6
    Views:
    279
    Hertha Steck
    Feb 12, 2007
  3. Navindra Umanee

    interacting with ruby program

    Navindra Umanee, Jan 31, 2005, in forum: Ruby
    Replies:
    21
    Views:
    274
    Navindra Umanee
    Feb 14, 2005
  4. Jeppe Jakobsen

    interacting with another program

    Jeppe Jakobsen, May 28, 2007, in forum: Ruby
    Replies:
    0
    Views:
    120
    Jeppe Jakobsen
    May 28, 2007
  5. Anthony M. Saffer

    Interacting w/another App on Win32

    Anthony M. Saffer, Apr 11, 2004, in forum: Perl Misc
    Replies:
    2
    Views:
    102
    Anthony M. Saffer
    Apr 12, 2004
Loading...

Share This Page