Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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'

In reply to IO::Tee not behaving correctly by grinder

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-23 15:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found