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/

In reply to Re: Restoring STDOUT and STDERR after having redirected them to files by isotope
in thread Restoring STDOUT and STDERR after having redirected them to files by isotope

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.