package Handler; use strict; use warnings; use Fcntl qw(:DEFAULT :flock); use IO::Handle; use Data::Dumper; my $opts = {   filename => './foo.bar',   mode => O_WRONLY | O_APPEND | O_CREAT,   umask => 0640, }; my $self = bless $opts, __PACKAGE__; open my $olderr, ">&STDERR" or die $!; $self->{olderr} = \$olderr; sysopen(STDERR, $self->{filename}, $self->{mode}, $self->{umask}) or die $!; $self->{fh} = \*STDERR; $self->{olddie} = $SIG{__DIE__}; $SIG{__DIE__} = \&_die_handler("bar foo"); STDERR->autoflush(1); sub _die_handler {   print {$self->{fh}} Dumper(\@_);   exit(9); } 1; die "foo bar";