Watch the behaviour of this code snippet in a current Perl (5.6):

        eval {
            print STDERR "Testing...\n";
            warn "Oops!";
            print STDERR "Still going...\n";
            die "Argh!!!";
            print STDERR "I died, didn't I?\n";
        };
        print STDERR "Outcome: $@\n";
-->
        Testing...
        Oops! at test.pl line 4.
        Still going...
        Outcome: Argh!!! at test.pl line 6.

Now try again, but with this *in front* of the eval block:

        $SIG{__DIE__} = sub {
            print STDERR "I got a fatal error: @_";
            print STDERR "Died.\n";
            exit;
        };

-->
Testing...
Oops! at test.pl line 10.
Still going...
I got a fatal error: Argh!!! at test.pl line 12.
Died.

The idea is to provide a way to do nice and clean fatal messages, e.g.
"fatals to browser" in a CGI environment.

But this messes up the behaviour of the eval: when the code inside the
eval block dies, the sub attached to $SIG{__DIE__} is executed, and the
script DOES die.

Putting a simple

        local $SIG{__DIE__};

at the start of the eval block rstores the old, and IMO desirable
behaviour.

My statement: this shouldn't be the programmer's responsibility. If
you're using other people's modules that depend on eval, you're in
trouble anyway. The "local $SIG{__DIE__};" statement should be implicit
at the start of the eval block.

-- 
        Bart.

Reply via email to