When your Perl program starts, it inherits two file handles for output, STDOUT and STDERR. Both go to the console with a bit of a difference, STDOUT is buffered and STDERR is not.

Run time warnings as well as anything that you code as warn "xy"; goes to STDERR. The default print goes to STDOUT. Many of my scripts just send their output to STDOUT (don't even open a file for output).

To cause the STDERR output to go to the same place, this can be done with the shell, to send the STDOUT and STDERR of something.pl into the more program...
perl something.pl 2>&1 | more
inside something.pl, the statement $|=1; will be important to turn off buffering so that the time sequence of prints to STDOUT and STDERR will be in order.

To cause the STDOUT and STDERR of something.pl to go to the same file: output.txt:
perl something.pl > output.txt 2<&1

To cause STDERR to go to some already opened file handle within a Perl program and also have this "work out right" in terms of output order is something that I hadn't done before and this took some fiddling. Below is my attempt at it and it appears to work. Normally you shouldn't have to do this! But "create a log file for errors and ...data" implies the need for something like this.

This is an abnormal situation -- at least it is a first time for me - Normally you want the "real output" to go one place and the "error messages" to go to another place!

#!/usr/bin/perl use warnings; use strict; use IO::Handle; open (my $fh, '>', 'logfile.txt') || die "can't open logfile.txt"; open (STDERR, ">>&=", $fh) || die "can't redirect STDERR"; $fh->autoflush(1); my $need_work = 5; my $i_tried = 0; while ( $need_work > $i_tried ) { my $a; $i_tried++; print "$a\n"; #this generates a run time warning!!!!!! warn "this is warning $i_tried"; #an explict warning print $fh "I've tried $i_tried things as a test\n"; } __END__ logfile.txt contains: Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 1 at C:\TEMP\junk2.pl line 19. I've tried 1 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 2 at C:\TEMP\junk2.pl line 19. I've tried 2 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 3 at C:\TEMP\junk2.pl line 19. I've tried 3 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 4 at C:\TEMP\junk2.pl line 19. I've tried 4 things as a test Use of uninitialized value $a in concatenation (.) or string at C:\TEM +P\junk2.pl line 18. this is warning 5 at C:\TEMP\junk2.pl line 19. I've tried 5 things as a test

In reply to Re: Creating log files for errors and warnings by Marshall
in thread Creating log files for errors and warnings by tej

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.