sub run_cmd { my $cmd = shift(); my $in; my $out; my $err; my $pid = open3($in, $out, $err, $cmd); return ($in, $out, $err); } my ($in, $out, $err) = run_cmd("some command"); while(<$out>) { do stuff with $out; } while(<$err>) { do stuff with $err; }