<?xml version="1.0" encoding="windows-1252"?>
<node id="165234" title="(RhetTbull) Re: duplicating STDERR to a logfile..." created="2002-05-08 22:03:51" updated="2005-07-19 14:08:11">
<type id="11">
note</type>
<author id="31063">
RhetTbull</author>
<data>
<field name="doctext">
My favorite way to handle this is with [cpan://Filter::Handle].  For example:
&lt;code&gt;
use warnings;
use strict;
use Carp;
use Filter::Handle qw/subs/;

open (LOGFILE, "&gt;logfile") or die "could not open logfile: $!";

#filter STDERR through an anonymous sub
Filter \*STDERR, sub {local $_ = "@_"; print LOGFILE "Filtered: $_ "; $_};

#need a signal handler to capture warnings from carp and warn which are not captured by Filter::Handle (but by capturing ourselves and printing to STDERR, they do get picked up by Filter::Handle)
$SIG{__WARN__} = sub {local $_ = "@_"; print STDERR $_};

#prints to both STDERR and to LOGFILE
print STDERR "error!\n";
carp "carp!";
warn "warn!";

#STDERR will no longer be filtered through your sub
UnFilter \*STDERR;

print STDERR "not captured to log\n";
warn "this one got away";

close LOGFILE;
&lt;/code&gt;

&lt;strong&gt;Update:&lt;/strong&gt;
Added signal handler to capture output of warn and carp
</field>
<field name="root_node">
165195</field>
<field name="parent_node">
165195</field>
</data>
</node>
