in reply to Process output redirecting

You could also do:
script.pl >logfile.txt 2>&1

Which will redirect the standard and error output to the logfile. That's the simple and easy way. Not the best way though.

Or you can:
close(STDOUT); close(STDERR); open(STDOUT , ">logfile.txt") or die "Can't open logfile: $!\n"; # now anything that is printed will go to the logfile unless # its given another filehandle. Just be sure to close the # handle before the program exits. ... # program end close(STDOUT);

Hope this answers your question.

BMaximus

Replies are listed 'Best First'.
Re: Re: Process output redirecting
by mr.nick (Chaplain) on Apr 23, 2001 at 04:38 UTC
    I second BMaximus' suggestion of redirecting a standard filehandle like STDOUT to a log file.

    However, I would suggest you keep STDOUT open as-is, and redirect STDERR instead. That way normal program output (via print) behaves like it always does, but warn and die output to your log file.

      Even better, use Tom Christiansen's implementation of Tie::Tee that he gives here...

      (There is also an IO::Tee, but it doesn't work like I would want. Yes, I am sending a bug report to the author.)