in reply to Re^2: Why eval {...};if ($@) { die $@ } else { ...???
in thread Why <c>eval {...};if ($@) { die $@ } else { ...</c> ???

What's that additional frame good for?

something() might be in some other module which you don't control. That technique allows you to delay the exit until the very line where you choose to die. An that's precisely what eval BLOCK is all about - delay the die, to mask it or die elsewhere.

Replies are listed 'Best First'.
Re^4: Why eval {...};if ($@) { die $@ } else { ...???
by Jenda (Abbot) on Apr 03, 2009 at 23:43 UTC

    I probably do not understand what you are trying to say. I know what's an eval BLOCK good for. I do not know what's an eval BLOCK;if ($@) {die $@}; good for.

      Well. As you know, the eval BLOCK is good for not dying. The eval BLOCK; die $@ if $@ is good for dying somewhere else, and not inside the code that's in the eval BLOCK. I don't know the specifics of that module you are talking about, but I've provided a simple example which shows the difference, even not adding anything to the argument to die but $@.

        So if I understand right ... you might want to die somewhere else just in case the $SIG{__DIE__} was set differently? Or maybe so that it's called several times?

        use Carp; $SIG{__DIE__} = sub {print STDERR "handler called\n"}; eval { eval { confess "blah\n"; }; if ($@) { die $@; } }; print $@ if $@;

        I don't think I would want that ever. Actually I think $SIG{__DIE__} should be avoided due to the way it's implemented. Let's see what does perlvar say:

        Due to an implementation glitch, the $SIG{__DIE__} hook is called even inside an eval(). Do not use this to rewrite a pending exception in $@, or as a bizarre substitute for overriding CORE::GLOBAL::die(). This strange action at a distance may be fixed in a future release so that $SIG{__DIE__} is only called if your program is about to exit, as was the original intent. Any other use is deprecated.