attach_stdout_filter; while (<>) { print; } sub attach_stdout_filter { my($pid); return if $pid = open(STDOUT, '|-'); defined $pid or die "Cannot fork: $!\n"; # child while () { s/foo/bar/; print; } }