in reply to Log message in Die handler

Because when you open a in handler it opens file and assigns to it descriptor with first available number -- 2 (STDERR).

Update: try this:

#!/usr/bin/perl use strict; use warnings; close STDERR; open FH, '>', 'log'; die "exit from program\n";

Update2: or more correct method:

#!/usr/bin/perl use strict; use warnings; use POSIX qw(dup2); open FH, '>', 'log'; dup2(fileno FH, 2); die "exit from program\n";

But actually I prefer using Log::Log4perl and its logdie method.

Update3: uhm, I just discovered, that when you open STDERR perl always assigns it to descriptor 2:

#!/usr/bin/perl use strict; use warnings; use POSIX qw(dup2); open FH, '>', 'log'; dup2(fileno FH, 2); open STDERR, '>', 'stderr'; # this would be placed into 'stderr', not into 'log' warn "STDERR descriptor is:", fileno(STDERR), "\n"; die "exit from program\n";

Replies are listed 'Best First'.
Re^2: Log message in Die handler
by gone2015 (Deacon) on Jan 16, 2009 at 13:36 UTC

    Good grief. This seems to imply that if you close STDERR then die (and other diagnostics ?) will happily be written to whatever happens to be attached as descriptor 2 ?

    If so, then the trick appears to be to open STDERR '>', '/dev/nul', or something else to occupy descriptor 2 to squelch output ? Thinking about a more portable solution I tried:

    use strict ; use warnings ; sub die_handler { open FH, ">&1"; print FH "die_handler: ", @_ ; } ; $SIG{__DIE__} = \&die_handler ; close STDERR ; open STDERR, '<', \'' ; die "Oh shoot !" ;
    which appears to do the trick. Noting that the close STDERR is required.