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

Enlightened Ones,

please lend me your wisdom!

I am extracting a lot of information from the web. During a trial run my internet connection scrambled for a while, leading to the error message "use of uninitalized value..." when the programme tried the process the data that it failed to download during this time. As I was sitting next to the screen I could monitor this, but the download will last for close to a day and naturally I do not want to be glued to the screen for quite so long (plus, I print out the current operation on the screen, so I cannot just come back to see if there was anything). To make it even more complicated, I also want to record where the problem occured. The files I am downloading are related to an internal number. Thus, I want to record the error message alongside this number. And finally, it would be nice to be able to see that the programme is running at the same time - which would exclude redirecting all output (i.e., also a print command with the current file) from the screen to a file. Oh, did I mention that I am using Windows (I know, I know, ...).

Long story, short conclusion: I want to continue the programme (so no die in connection with the get for this particular problem), but create a logfile listing all error messages (not just the ones related to a failure of a get-command or the opening of files, etc.). Besides the error messages it should also include the number of the file where the error occured. This should not interfere with other reading/writing operations, in particular not with printing standard output to the screen. The general idea is to keep the programme running, monitoring its progress and fix errors with some files later.

Is there a way to write all (non-fatal) error messages occuring during the execution of a programme into a file? Would this interfere with any reading/writing operation on other file (for a construction opening a logfile in the beginning and closing at the very end)?

Thanks in advance!

Replies are listed 'Best First'.
Re: Logfile for Error Messages
by wazoox (Prior) on Dec 03, 2004 at 17:46 UTC
    Well you just want to redirect STDERR to some file. The could do this from the shell of course (unless you're running windows, phew), this way :

    $./myscript.pl 2>/some/log/file

    of course you can manage it from your script :
    open ( STDERR , '>>/some/log/file') or die "Can't open the log file! : $!";
    The '>>' sign will append errors to the existing file; '>' will overwrite the previous log.
Re: Logfile for Error Messages
by TedYoung (Deacon) on Dec 03, 2004 at 17:55 UTC

    Hi,

    If you are running under a shell that does not offer redirection (e.g. Windows Command), then you can also do this:

    BEGIN { open STDERR, ">>somefile.txt"; # use > for overwrite, >> for appe +nd }

    Ted Young

    ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)
      WindowsNT command shell offers redirection via  > and  |

        I have been using cygwin so long, I forgot what the native consol offers.

        Ted Young

        ($$<<$$=>$$<=>$$<=$$>>$$) always returns 1. :-)