http://www.perlmonks.org?node_id=661393

stevendel has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to redirect STDOUT and STDERR to a file as well as to STDOUT or STDERR (depending on the intended destination). I am familiar with IO::Tee, so what's the problem? I want to write this in such a way that any messages, including runtime errors thrown by modules outside of my control, are sent to both places. The snippet below should show what I am looking for (and what has failed for me thus far):
use IO::Tee; use IO::File; open(OLDOUT, ">&STDOUT"); open(OLDERR, ">&STDERR"); my $output_file = new IO::File(">tt1.out"); my $TeeOut = new IO::Tee(\*OLDOUT, $output_file); my $TeeErr = new IO::Tee(\*OLDERR, $output_file); open(STDERR, ">&", \*$TeeErr); select(STDERR); $| = 1; print $TeeErr "This goes to both STDERR and to the file.\n"; print STDERR "I would like this to do the same as above, but it goes n +owhere.\n";
Any thoughts/suggestions?