On Jan 18, 2008 2:45 PM, Andy Greenwood <[EMAIL PROTECTED]> wrote:
snip
> > $SIG{__DIE__} = sub {
> > open my $fh, ">>", "something.log"
> > or die @_, "could not open something.log: $!";
> > print $fh @_;
> > };
> >
> > die "Oops";
> >
>
> Would this not be susceptible to infinite recursion if it fails to open
> something.log? Would it not be safer/better to do something like this
> (untested)?
snip
Did you try it? Here, this code will trigger the recursion you are
worried about
#!/usr/bin/perl
use strict;
use warnings;
$SIG{__DIE__} = sub {
open my $fh, ">>", "something.log"
or die @_, "could not open something.log: $!";
print $fh @_;
};
#create something.log if it doesn't exist
open my $fh, ">>", "something.log"
or die "could not open something.log: $!";
close $fh;
chmod 0000, "something.log";
die "Oops";
And the reason it works is in perldoc perlvar:
The routine indicated by $SIG{__DIE__} is called when a fatal
exception is about to be thrown. The error message is passed
as the first argument. When a __DIE__ hook routine returns,
the exception processing continues as it would have in the
absence of the hook, unless the hook routine itself exits via a
"goto", a loop exit, or a die(). The "__DIE__" handler is
explicitly disabled during the call, so that you can die from a
"__DIE__" handler. Similarly for "__WARN__".
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/