in reply to Restoring STDOUT and STDERR after having redirected them to files

chipmunk accurately identified the source of my problem. I had thought I was copying the filehandles, but I was actually dup'ing globs, so I was stomping the filehandle value anyway. Unfortunately, the fileno approach is only useful if I use it everywhere in my code, which I didn't, for other reasons. I did, however, manage to put together a solution that works exactly as desired, massaging things into the approach suggested in perlfunc:open:
# Save the previous filehandles for STDOUT and STDERR so we # can restore them when we're done. local *REAL_STDOUT; local *REAL_STDERR; open(REAL_STDOUT, ">&STDOUT"); open(REAL_STDERR, ">&STDERR"); $this->{'_stdout'} = *REAL_STDOUT; $this->{'_stderr'} = *REAL_STDERR; # Set the new filehandles for STDOUT and STDERR local *STDOUT_LOG; local *STDERR_LOG; *STDOUT_LOG = $this->{'stdout_log'}; *STDERR_LOG = $this->{'stderr_log'}; open(STDOUT, ">&STDOUT_LOG"); open(STDERR, ">&STDERR_LOG");
I can restore STDOUT and STDERR later with this code:
if(defined($this->{'_stdout'})) { local *REAL_STDOUT; *REAL_STDOUT = $this->{'_stdout'}; open(STDOUT, ">&REAL_STDOUT") or warn "Couldn't restore STDOUT: $! +"; print STDOUT "STDOUT restored\n"; undef($this->{'_stdout'}); } if(defined($this->{'_stderr'})) { local *REAL_STDERR; *REAL_STDERR = $this->{'_stderr'}; open(STDERR, ">&REAL_STDERR") or warn "Couldn't restore STDERR: $! +"; print STDOUT "(STDERR restored)\n"; print STDERR "STDERR restored\n"; undef($this->{'_stderr'}); }
Everything's happy now.

--isotope
http://www.skylab.org/~isotope/