A colleague was creating a process that write to a log file. While debugging his app he wanted to output his log entries to the display rather than his log file. So I came up with this to keep him from re-doing all his print statements.

This is my 1st snippet posting so please be gentle.

$debug = 1; # toggle to 0 to turn off debug if ($debug) { open(FH, ">-") or die "Unable to open STDOUT: $!\n"; } else { open(FH, ">/tmp/var/$0$$.log") or die "Unable to open $0$$.log $!\ +n"; } print FH "If \$debug = 1 this will print to log otherwise print to the + screen(STDOUT)\n";

Replies are listed 'Best First'.
Re: Printing "log" output while debugging
by ajdelore (Pilgrim) on Aug 26, 2003 at 17:30 UTC

    Depending on how you are using this in the script, you may want to add:

    select FH;

    This will make FH the default for all print statements, so you won't need to use print FH each time.

    </ajdelore>

      Great pointer ... Never thought of that++
Re: Printing "log" output while debugging
by ctilmes (Vicar) on Aug 26, 2003 at 18:17 UTC
    You can also just "reopen" STDOUT to point it at the file:
    $debug = 1; # toggle to 0 to turn off debug if ($debug) { open(STDOUT, ">/tmp/var/$0$$.log") or die "Unable to open $0$$.log + $!\n"; } print "If \$debug = 1 this will print to log otherwise print to the sc +reen(STDOUT)\n";
Re: Printing "log" output while debugging
by Aristotle (Chancellor) on Aug 28, 2003 at 12:19 UTC
    Just use Log::Log4Perl. It has a great :EASY mode for simple scripts.

    Makeshifts last the longest.