use strict; use IPC::Open3; use IO::Select; use Carp; my $cmd = "ls -al"; my $pid = open3( *cmdIn, *cmdOut, *cmdErr, $cmd) or die "can not open $cmd $!"; print "\$pid $pid"; print "\n"; $SIG{CHLD} = sub { print "result: Status $? in $pid\n" if waitpid($pid, 0) >0 }; my $select = IO::Select->new(*cmdErr, *cmdOut); print cmdIn "\n\n$cmd\n"; close(cmdIn); my $input; while ( my @ready = $select->can_read()) { foreach my $fh ( @ready ) { if (fileno($fh) == fileno(cmdErr)) { process_errors(); } else { print "STDOUT: ", <$fh>; } $select->remove($fh) if eof($fh); } } # close (cmdOut); close(cmdErr); sub process_errors { my $select= IO::Select->new( *cmdErr ); my $input; while ( $select->can_read(0.1) ) { sysread *cmdErr, (my $line), 1024; last unless $line; $input.= $line; } croak "THERE: $input $cmd code" if $input =~ /error:/; carp "HERE: $input $cmd code" if $input; } { local $SIG {__WARN__} = sub { my $warning = shift ; # warning handler }; # commands }