Q:

Die if anything is written to STDERR?

How can I force Perl script to die if anything is written to STDERR ?
Such action should be done instantly, when such output happen, or even before, to prevent that output... 

A:

This doesn't seem like an especially smart idea, but a tied filehandle should work. According to the perltie manpage:

When STDERR is tied, its PRINT method will be called to issue warnings and error messages. This feature is temporarily disabled during the call, which means you can use warn() inside PRINT without starting a recursive loop. 

So something like this (adapted from the manpage example) ought to work:
package FatalHandle;

use strict;
use warnings;

sub TIEHANDLE {  my $i; bless \$i, shift }

sub PRINT { 
    my $r = shift; 
    die "message to STDERR: ", @_;
}

package main;

tie *STDERR, "FatalHandle";

warn "this should be fatal.";

print "Should never get here.";

And that outputs (with exit code 255):
message to STDERR: this should be fatal. at fh.pl line 17.

A:

Here's a method that works no matter how STDERR (fd 2) is written to, even if it's a C extension that doesn't use Perl's STDERR variable to do so. It will even kill child processes that write to STDERR!
{
   pipe(my $r, my $w)
      or die("Can't create pipe: $!\n");
   open(STDERR, '>&', $w)
      or die("Can't dup pipe: $!\n");
   close($r);
}

print "abc\n";
print "def\n";
print STDERR "xxx\n";
print "ghi\n";
print "jkl\n";

 
$ perl a.pl
abc
def

$ echo $?
141

Doesn't work on Windows. Doesn't work if you add a SIGPIPE handler.

