http://www.perlmonks.org?node_id=900389


in reply to Easy way to capture STDOUT and STDERR without IPC

If you don't need the entire string and you'd rather loop you can do something like this:
open my $CMD, '-|', 'ls 2>&1' or die $!; while (<$CMD>) { print if /\.xml/; } close $CMD or die $! ? 'Pipe failed' : 'Program error: ' . ($? >> 8) ;