I understand, and the OP code (once I grokked it) actually struck me as more clever than obfuscated. Nice, even.
But if you really want completely unbuffered output to the log file and anywhere else, you have to use syswrite and sysread, and you have to use those exclusively. The standard i/o methods (i.e. print and <FILEHANDLE>) are intrinsically line-oriented --
Well, actually, print and the diamond operator are record oriented, where the record delimiter is defined by the globals $/ and $\. So you could try playing with those -- but that won't be any less obfuscative than just using sysread and syswrite.
I had to play with it a bit, but the following version of the OP code does what I think you want in terms of making sure that output passes through the pipe and into the log file with the shortest possible buffering delay.
(Note that you cannot avoid doing word-level buffering, because without that, you wouldn't be able to use s/// to remove the password string. Also, a lot of stuff in the "internal" script needs to be escaped in order to work right -- I was actually puzzled that "\s" had to be "\\s" in the "if" clause, but doing "\\Q" and "\\E" was apparently unnecessary.)
I agree that having to stuff all those backslashes into the internal script is really ugly, so maybe you'll want to externalize that part of the code into a separate script file, as suggested in the first reply. (But that means storing your password string in yet another file, so as not to expose it in the system's process table.)use warnings; use strict; my $password = "hide_me"; my $log_file = "test.log"; open STDOUT, "| perl | tee -a $log_file"; my $script = <<_END_FILTER_SCRIPT_; use warnings; use strict; my \$chr; \$_ = ''; while (sysread DATA, \$chr, 1) { \$_ .= \$chr; if ( /\\s\$/ ) { s/\Q$password\E/removed/ig; syswrite STDOUT, \$_; \$_ = ''; } } close STDOUT; __DATA__ _END_FILTER_SCRIPT_ syswrite STDOUT, $script; sleep 1; ## had to add this delay so next syswrite would always work syswrite STDOUT, "The password is $password\n"; foreach (qw{Output is unbuffered if these words are printed one at a t +ime.}) { syswrite STDOUT, $_." "; sleep 1; } syswrite STDOUT, "\n"; sleep 1; syswrite STDOUT, "Output is line buffered if the last line came out al +l at once.\n"; close STDOUT;
(update: removed unnecessary "binmode" calls)
In reply to Re: Filtering passwords from the output of a script
by graff
in thread Filtering passwords from the output of a script
by quester
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |