eval BLOCK catches die. The following catches warnings and other uses of STDERR:
use v5.8.0;
my $stderr;
{
open(local *STDERR, '>', \$stderr);
warn("foo\n"); # no output
}
print STDERR ("\$stderr: $stderr"); # $stderr: foo
Together:
use v5.8.0;
my $stderr;
eval {
open(local *STDERR, '>', \$stderr);
warn("foo\n"); # no output
die("bar\n"); # no output
};
print STDERR ("\$stderr: $stderr"); # $stderr: foo
die($@) if $@; # bar
In earlier version of Perl, I think you can use IO::Scalar to do:
tie *STDERR, 'IO::Scalar', \$stderr;