There was (brief) discussion yesterday about how to use tied filehandles. And I put up an example of using a tied filehandle to filter STDOUT, but I didn't really like it because, well, it ended up printing to STDERR. :)

So I came up with this, which is nicer, because at least it prints to the filehandle you've specified. But to do that I have to untie the filehandle before I print to it; otherwise I'll end up writing to my tied filehandle, which will write to my tied filehandle, and so on. Deep recursion. :)

So I've implemented this, which first untie's the filehandle, then tie's it back up again afterwards. Which is kind of ugly, particularly since I've had to turn off warnings to get rid of the "untie attempted while 1 inner references still exist" error. Which perhaps means that I'm leaking memory.

Any ideas on cleaning that bit up would be appreciated.

Anyway, usage:

use Filter::Handle; my $f = Filter::Handle->new(\*STDOUT); print "Foo"; print "Bar";

You don't have to use STDOUT, of course; that's the whole idea. Any filehandle will do.

package Filter::Handle; use strict; sub new { my $class = shift; my $fh = shift; tie *{$fh}, 'Filter::Handle::Tie', $fh; bless { fh => $fh }, $class; } sub DESTROY { my $self = shift; my $fh = $self->{fh}; undef $self; { local $^W = 0; untie *{$fh} } } package Filter::Handle::Tie; use vars qw/@ISA/; use Tie::Handle; @ISA = qw/Tie::Handle/; sub TIEHANDLE { my $class = shift; my $fh = shift; bless { fh => $fh }, $class; } sub PRINT { my $self = shift; my $fh = $self->{fh}; my($file, $line) = (caller)[1,2]; { local $^W = 0; untie *{$fh} } print $fh sprintf "%s:%d - %s\n", $file, $line, join ' ', @_; tie *{$fh}, __PACKAGE__, $fh; } 1;

In reply to 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.