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

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.

Replies are listed 'Best First'.
Re^5: Why eval {...};if ($@) { die $@ } else { ...???
by shmem (Chancellor) on Apr 04, 2009 at 00:04 UTC

    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.