http://qs321.pair.com?node_id=27415

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;