Doesn't that only redirect the STDERR of sed, not of $cmd? In any case, you're better off using IPC::Open3 (read perlipc on how to manage children), do your transformation in perl, and merge your streams (check out select(RBITS,WBITS,EBITS,TIMEOUT) or IO::Select) in perl.