Hey now, 21 years after this inspired module was added, I just wanted to note a small bug in the Filter subroutine (in package Filter::Handle). It should be:
sub Filter { my $fh = shift; tie *{ $fh }, __PACKAGE__, @_; }
Note how the subroutine UnFilter uses the shift correctly. Also, it's a shame that this is no longer on CPAN. But this chunk of module is enough to put into one's "private" library for use. Using a CODE reference, you can duplicate all your STDOUT and STDERR (including any that comes out of perl warnings), like the following:
select STDERR; $|=1; select STDOUT; $|=1; use FileHandle; my $LOG_FH = new FileHandle($logfile, "w"); $LOG_FH->autoflush; open(DUPOUT, ">&STDOUT") or die "Couldn't dup STDOUT: $!\n"; open(DUPERR, ">&STDERR") or die "Couldn't dup STDERR: $!\n"; use Filter::Handle qw/subs/; our $FILTER_STDOUT = sub { local $_ = "@_"; print DUPOUT $_; sprintf "[STDOUT]: %s", "@_" if (defined $LOG_FH && $LOG_FH->opened); }; our $FILTER_STDERR = sub { local $_ = "@_"; print DUPERR $_; sprintf "[STDERR]: %s", "@_" if (defined $LOG_FH && $LOG_FH->opened); }; ## Call Filter to tie the filehandles ## Call UnFilter to untie the filehandles (don't care) Filter \*STDOUT, $LOG_FH, $FILTER_STDOUT; Filter \*STDERR, $LOG_FH, $FILTER_STDERR;
I like this much better than IO::Tee, because I don't need a custom filehandle to print to to get a logfile of all output. I'm continuing to play around with this and may update this thread more later.

In reply to Re^2: Filehandle Filter by cadphile
in thread Filehandle Filter by btrott

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.