http://qs1969.pair.com?node_id=151332

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

Fellow monks, I have a really hairy problem with IO::Tee that I can't resolve. I am using IO::Tee to print to STDOUT and a log file at the same time. In this way, anything that is written to STDOUT is, theoretically, guaranteed to be written to the log file as well, which comes in handy when I have to trace why something didn't work correctly.

The problem is that at the end of the program, the log file is of length zero, and yet everything is correctly printed to STDOUT. I created a test program, but that works flawlessly. It goes something like this:

#! /usr/bin/perl -w use strict; # use all the modules I'm using in the main script, just in case use DBI; use File::Basename qw/basename/; use File::Path qw/mkpath/; use Getopt::Mixed; use IO::File; use IO::Tee; my $max = shift || 3; BEGIN { my @now; sub work_name { @now = localtime unless @now; sprintf( '/var/log/test/%02d%02d%02d.log', sub{ @_[reverse 0..2] }->(@now) ) } } my $logger = do { my $logfile = work_name(); my $fd = new IO::File( "> $logfile" ) or die "Could not open $logfile for output: $!\n"; new IO::Tee(\*STDOUT, $fd ); }; END { $logger and $logger->flush and $logger->close } for( 1..$max ) { my $letter = ('a'..'z')[rand 26]; my $lines = 1 + int(rand(10)); my $cols = 1 + int(rand(72)); $logger->print( "$_ ", (($letter x $cols) . "\n") x $lines ); } $logger->print("--end--");

I thought it might have been problems with a missing \n on the last line, or needing to flush explicitly, but that doesn't change things. The test script works, but not the main script. Elsewhere in the script I am using arrays of arrays of IO::File objects which behave as advertised. At no point do I do any bare prints (to STDOUT), everything is printed via the logger object.

If, in the main script, I create the $logger object, print to it and exit immediately, the output gets recorded in the log file. Something down the track seems to be making things go haywire, and I am at a loss as to explain why. I am using 5.6.0, which is the most suspicious thing I can think of. For instance, I am not using sfio.

Any tips or suggestions for things to try are most appreciated.


print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'