Date sent:              Mon, 22 Jun 2009 10:53:06 -0700
From:                   pa...@compugenic.com
To:                     beginners@perl.org
Subject:                redirecting STDERR with IO::Tee

> I have a script which runs mostly via a cron job and sometimes
> interactively.  I would like STDERR to automatically print to both the
> console and to a logfile simultaneously. 
> 
> Right now I've gotten as far as merging both file handles with IO::Tee
> but I'm not sure if I'm heading down the right path. 
>
> my $merged_stderr = IO::Tee->new( \*STDERR, new IO::File(">>$errlog") );
> 
> This only works when explicitely naming the file handle in a print
> statement.  How can I take it to the next step, which is to have
> STDERR automatically print to that file handle? 

#!perl
use strict;
use IO::Tee;

warn "Before the change";
open my $oldSTDERR, '>&STDERR' or die;
close STDERR;
open my $DBG, '>', 'debug.log' or die;
*STDERR = IO::Tee->new( $oldSTDERR, $DBG) or die;

warn "after the change";

print "to STDOUT\n";
print STDERR "to STDERR\n";

system ('dir sdf_sgerdfwerg');

die "Bleargh";
__END__

it's not possible to tee (this way) the STDERR of the child 
processes, the best you can do is to decide whether you want them to 
go to the file or the ordinary error output. Try to escape the "close 
STDERR;".

HTH, Jenda
===== je...@krynicky.cz === http://Jenda.Krynicky.cz =====
When it comes to wine, women and song, wizards are allowed 
to get drunk and croon as much as they like.
        -- Terry Pratchett in Sourcery


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to