Quote:
Originally Posted by jerardfjay
I was hoping there would be something similar in PERL to perform the redirection within the program instead of actually using the command line redirection (for example we can use exec >> $LOGFILE 2>&1 in shell programming). Is there any such equivalent to redirect STDOUT and STDERR to a logfile within PERL program? Please advise. Thanks.
Jerardfjay
There is a way to do this in PERL. You could open a log file for writing. and since the default file handle for print commands is always the currently selected file handle, you can use the
select the file handle for the log file before you use your print commands. This way you are directing all of your print command outputs to the log file instead of the console.
open (LOGFILE, $logfile);
select (LOGFILE);
print 1;
.
.
print 2;
.
.
The only issue concern that I have for this method is that, I don't believe that STDERR will be redirected to the logfile. Note to keep in mind is that "select (Filehandle)" is a stick operation. It works well in this situation, however if you have a need to make interaction with the user from within the program you have to explicitly specify select (STDOUT) to redirect output to console again.
Jerardfjay