IO::Select; pipe my $stdout_read, my $stdout_write; pipe my $stderr_read, my $stderr_write; my $pid = fork // die; if ($pid == 0) { close $stdout_read; close $stderr_read; open STDOUT, ">&=", $stdout_write; open STDERR, ">&=", $stderr_write; exec {"perl"} "perl", "-le", '$|++; for (1..10) { print $_; warn $_; sleep 1 }'; } close $stdout_write; close $stderr_write; my $select = IO::Select->new($stdout_read, $stderr_read); while ($select->count) { for my $fh ($select->can_read) { my $len = sysread $fh, my $str, 1024; if ($len) { my $type = $fh == $stdout_read ? "out": "err"; print strftime("%FT%T", localtime) . " $type $str"; } else { $select->remove($fh); close $fh; } } } wait; select!