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

Dear Masters,

Is there a way to do it? Such that when I piped the script into a file like below, I still can read the warning message in the debug.txt file.
$ perl mycode.pl > debug.txt


---
neversaint and everlastingly indebted.......

Replies are listed 'Best First'.
Re: Printing Warning Messages Into a File
by radiantmatrix (Parson) on Oct 28, 2005 at 14:20 UTC

    Warning messages don't go to STDOUT, and the redirection you're using only redirects STDOUT to a file. This is typically a good thing, since it's nice to be able to have all of your prints, for example, be saved in a file while your warnings are not.

    You can either use the shell to redirect such warnings to a file, as others have explained, or in your code you can set up a signal handler for warnings:

    open WARN, '> warnings.txt' or die ("Cannot write warnings.txt: $!"); local $SIG{__WARN__} = sub { print WARN @_,"\n" };

    Or, even easier IMO, redirect STDERR, so that everything written there (which includes warnings) will be written to a file.

    open WARN, '> warnings.txt' or die ("Cannot write warnings.txt: $!"); *STDERR = *WARN;
    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    "In any sufficiently large group of people, most are idiots" - Kaa's Law
Re: Printing Warning Messages Into a File
by EvanCarroll (Chaplain) on Oct 28, 2005 at 02:20 UTC
    The shorthand to do so in bash or another console that supports it is:
    $ perl mycode.pl &> debug.txt
    or the more explicit:
    $ perl mycode.pl 2>1 1> debug.txt
    Both of these redirect streams STDERR and STDOUT to a file.

    Also see:
    open 'STDERR', '>', './File' or die $!;
    and:
    open 'STDERR', '>', './File' or die $!;
    Which would make it so the streams are rediected inside of the program rather than by the console.


    Evan Carroll
    www.EvanCarroll.com
      $ perl mycode.pl 2>1 1> debug.txt

      No, that's broken in two ways. First, you are missing an ampersand... (which means your errors are going into the file ./1.) After you fix that, you'll still see the error. Pop quiz: Why?

      -sauoq
      "My two cents aren't worth a dime.";
      
        God, I always wondered why that was and I keep getting it wrong. (the part in the spoiler I mean) But your explanation makes sense!