in reply to Can't capture STDERR to Variable

Here is a general purpose example using IPC::Open3. You can run your exact command, instead of bash in the example.
#!/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: Can't capture STDERR to Variable
by ikegami (Patriarch) on Jul 21, 2011 at 19:31 UTC

    open3 is a pretty low-level tool. It does some things quite well, but this isn't one of them. See instead: IPC::Run3, IPC::Run

    PS — Your code has a race condition that could deadlock both processes since you only passed two of the three handles to select.