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;
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.