Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: Getting Call-Response Behavior with Expect

by zentara (Archbishop)
on Sep 23, 2010 at 10:49 UTC ( #861505=note: print w/replies, xml ) Need Help??

in reply to Getting Call-Response Behavior with Expect

You may be able to use IPC::Open2 or IPC:Open3 to simplify your code.

For example, try running this:

#!/usr/bin/perl use warnings; use strict; use IPC::Open3; use IO::Select; my $pid = open3(\*WRITE, \*READ,\*ERROR,"/bin/bash"); my $sel = new IO::Select(); $sel->add(\*READ); $sel->add(\*ERROR); my($error,$answer)=('',''); while(1){ print "Enter command\n"; chomp(my $query = <STDIN>); #send query to bash 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); # 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.
Old Perl Programmer Haiku ................... flash japh

Replies are listed 'Best First'.
Re^2: Getting Call-Response Behavior with Expect
by gnosti (Hermit) on Sep 23, 2010 at 15:19 UTC

    That's helpful snippet. Just from running that code with the target CL app, I see I wasn't distinguishing between output that came via STDERR and STDOUT in my Expect example.

    I read about IPC::Open2/3 before, and briefly gandered IPC::Run, so I don't know what I got waylaid by Expect.

    I guess I expected something more... Also, the 'select' call is something new to me. Worth learning about.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://861505]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2018-03-20 05:02 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (247 votes). Check out past polls.