Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2024-04-19 11:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found