$|=1; attach_stderr_filter(); while (<>) { print "To STDOUT: $_"; print STDERR "To STDERR: $_"; } sub attach_stderr_filter { my($pid); # This is the top secret part :) $pid = open(STDERR, '|-'); defined $pid or die "Cannot fork: $!\n"; if($pid) { return; } # child, now prints to STDERR. while () { s/foo/bar/; print STDERR "$_"; } }