I'm looking for a way to tee STDERR so that all output to it also goes to a file. I'm using ActivePerl v5.20.2 for Windows. I tried code suggested in the topic https://www.perlmonks.org/?node_id=843501. It does tee STDERR, but not to the file I want. It creates a file named "GLOB(0xf7b278)" and the output goes to that file. It also creates the file I specified, but it's empty
use strict;
use warnings;
use open qw(:std :utf8); # Set the default encoding for STDIN, STDOUT
+& STDERR to UTF-8
use IO::Tee ();
my $logname = 'test-tee$.log';
open(my $logfile, ">:encoding(UTF-8)", $logname) or die qq(Can't open
+"$logname for writing"\n);
# Tee STDERR to log file. If running as Administrator, console window
+may disappear quickly!
open STDERR2, ">&=STDERR" or die "Failed to alias STDERR: $!";
*STDERR = IO::Tee->new(\*STDERR2, ">$logfile") or
die "Failed to tee to aliased STDERR and '$logfile': $!";
print(STDERR "This is a test\n");
Any suggestions? Thanks in advance.
UPDATE: I found the problem. Line 9 should be:
*STDERR = IO::Tee->new(\*STDERR2, $logfile)
I thought I had tried that also, but apparently not!
-
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.