The trick is to use a local'ized version of STDERR, in the scope that interests you:
#! /usr/bin/perl -w
use strict;
warn "initial write to STDERR\n";
# open a scope
{
local *STDERR;
open STDERR, '>tmp.stderr' or die "Cannot open tmp.stderr for output
+: $!\n";
warn "redirected write to STDERR\n";
close STDERR;
}
warn "final write to STDERR\n";
You could probably get away without having the close STDERR at the end of the scope, but it's much cleaner (easier to understand what's going on) to do so explicitly.
Later: I just went and checked the example as per danger's suggestion. The technique there seems to be a little more complicated. You basically open up another file handle to cache a copy of STDERR, and then when you are finished you use that to reset the original STDERR back again. Which means there's some baggage you have to drag along with you (the previous STDERR descriptor). Using local hides this for you => one less damned thing to go wrong.
--g r i n d e r
|