This is not supposed to be obfuscated...

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.)

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;
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.)

(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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.