in reply to STDERR Restore after redirect

Why save and restore when local() will do that for you.

#!/usr/bin/perl # http://perlmonks.org/?node_id=1213644 use strict; use warnings; warn "before on STDERR\n"; test(); warn "after on STDERR\n"; sub test { my $log = ''; local *STDERR; open STDERR, '>', \$log or die; warn "test message should be in log\n"; close STDERR or die; print "log contents: $log"; }

Outputs:

before on STDERR log contents: test message should be in log after on STDERR

Replies are listed 'Best First'.
Re^2: STDERR Restore after redirect
by tultalk (Monk) on Apr 27, 2018 at 03:54 UTC

    Thanks

    Had already tried that with same result

    Mental block on scoping

    Have block of code that calls a sub which calls another sub which returns result(0 or 1) after allegedly closing the handle opened in that last sub. The result propagates back to the original block which then tries to print a response back to the client. That print command is what comes up against the closed handle. The close was after STDERR AND STDOUT were restored so I can't understand how the STDOUT was closed.

    If the handles are closed, perhaps reopening them.

      Anything anywhere in the SendMail package could have done it. I think you need to take a different approach to this.