STDOUTand STDERR- these are only file descriptors that are initialized with the standard output and error of your program, but they can be reassigned at any time for any reason. To do this, you want to keep the original settings in order to restore them.
sub process_that_writes_to_temp_log {
*OLD_STDOUT = *STDOUT;
*OLD_STDERR = *STDERR;
open my $log_fh, '>>', '/logdirectory/the-log-file';
*STDOUT = $log_fh;
*STDERR = $log_fh;
*STDOUT = *OLD_STDOUT;
*STDERR = *OLD_STDERR;
}
, , STDOUT/STDERR , local.
sub routine_that_writes_to_log {
open my $log_fh, '>>', 'log-file';
local *STDOUT = $log_fh;
local *STDERR = $log_fh;
}