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 } #### /home/murcia # ./wrapper.pl $pid 11685 <<<<< OK STDOUT: total 48 -rwxr-xr-x 1 murcia users 77 Dec 14 11:30 test.pl result: Status 0 in 11685 /home/murcia # ./wrapper.pl $pid 11687 <<<< not OK /home/murcia #