Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^2: Filehandle Filter

by cadphile (Beadle)
on Nov 23, 2021 at 00:10 UTC ( [id://11139034]=note: print w/replies, xml ) Need Help??


in reply to RE: Filehandle Filter
in thread Filehandle Filter

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.

Replies are listed 'Best First'.
Re^3: Filehandle Filter
by choroba (Cardinal) on Nov 23, 2021 at 17:19 UTC
    What version of Perl are you using? The original usage example
    use Filter::Handle qw/subs/; Filter \*STDOUT, sub { "Foo: @_\n" }; print "Bar"; UnFilter \*STDOUT;
    works for me correctly in 5.6.2, but fails in 5.10.1 with
    Deep recursion on subroutine "Filter::Handle::PRINT" at .../lib/Filter +/Handle.pm line 51. Segmentation fault (core dumped)

    When I replace $_[0] with shift, it fails in all the versions from 5.6.2 to blead:

    Not a GLOB reference at .../lib/Filter/Handle.pm line 50. (in cleanup) Not a GLOB reference at .../lib/Filter/Handle.pm line + 25 during global destruction.

    > it's a shame that this is no longer on CPAN

    See Text::OutputFilter.

    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11139034]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2024-04-26 02:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found