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

O Wise Monks, I'm using Active State on WinXP, where this little script causes an error, outputted to my msDos command window.
#errorToOut.pl use strict; use warnings; #causes error. print $forgotToDeclareThis
I'd like to capture all errors to a text file to review later. This is for situation I'm having in another program, where I get so many error messages so fast that they scroll of the command line before I can type ctrl-c, so I can't get back to the first error, which is probably the root of all the later evil.

I tried perl errorToOut.pl > output.txt but this simpleminded little attempt of mine didn't work.

Wise monks, can someone help me with this?

Replies are listed 'Best First'.
Re: How do I output error messages to a file?
by gellyfish (Monsignor) on Jan 26, 2005 at 11:14 UTC

    I think that 2>output.txt will work on XP.

    /J\

      The OP will want to ammend their code to print the errors to STDERR so that they can be redirected with >2 output.txt. Normal output is still printed to STDOUT (usually the console).

      E.g.

      print STDERR "This didn't work as expected!\n";

        I think the OP was talking about warnings and/or strict errors, which already do go to STDERR but yes if they want to catch their own messages then these will have to be either explicitly printed to STDERR or output with warn.

        /J\

Re: How do I output error messages to a file?
by jweed (Chaplain) on Jan 26, 2005 at 11:26 UTC
Re: How do I output error messages to a file?
by Frantz (Monk) on Jan 26, 2005 at 11:41 UTC
    You can try File::Log
    with 'stderrRedirect => 1' when you buid your object

    See File::Log
Re: How do I output error messages to a file?
by tphyahoo (Vicar) on Jan 26, 2005 at 12:25 UTC
    Thanks. My vote for the best solution is the first one.
    perl errorToOut.pl 2> output.txt
    Simple, a bit cryptic, but it works.
      2>file is a pretty standard way to redirect errors. Unix has been doing it since the dawn of time.

      --
      /renz.
      "I often wonder if I really need all of these bones." --Sean Stolon.
      On second thought, I am beginning to see the beauty of
      open STDERR, ">", "/path/to/log";
      which I will be coupling this with
      open STDOUT, ">", "path/to/log";
      I debug more with print statements than with the perl debugger, so this allows me to see my debugging statements along with the more serious perl complaints to see where my code is acting up.

      Thanks again to everyone who enlightened me.

      UPDATE: Here's a simple demonstration that may be helpful to newbies.

      #errorToOut.pl use strict; use warnings; open STDERR, ">>", "log.txt"; open STDOUT, ">>", "log.txt"; #outputted to stderr warn "warning!"; #outputted to stdout print "Test."; #Log.txt winds up looking like: #warning! at errorToOut.pl line 9. #Test.
Re: How do I output error messages to a file?
by ambrus (Abbot) on Jan 26, 2005 at 14:57 UTC

    Reopening STDERR is right, but there is an other solution too, using $SIG{__DIE__} as follows (untested code):

    BEGIN{ open LOGFILE, ">>", "/path/to/log" or die "cannot open logfile: $!"; $SIG{__DIE__}= sub { print LOGFILE $_[0]; } $SIG{__WARN__}= sub { print LOGFILE $_[0]; } }