use warnings; use strict; my $password = "hide_me"; my $log_file = "test.log"; open STDOUT, "| perl | tee -a $log_file"; select STDOUT; $| = 1; print <<_END_FILTER_SCRIPT_; use warnings; use strict; select DATA; \$|=1; select STDOUT; \$|=1; while () { s/\Q$password\E/removed/ig; print; } __DATA__ _END_FILTER_SCRIPT_ print "The password is $password\n"; foreach (qw{Output is unbuffered if these words are printed one at a time.}) { print $_, " "; sleep 1; } print "\n"; sleep 1; print "Output is line buffered if this appears one second after the last line.\n"; close STDOUT; #### The password is removed Output is unbuffered if these words are printed one at a time. Output is line buffered if this appears one second after the last line.