slloyd has asked for the wisdom of the Perl Monks concerning the following question:

Is it possible to call exit in an eval statement to only exit the eval statement? die seems to work this way.

Works:

eval {die;}; if (my $e = $@) { # <--- secure... warn "Got the exception '$e'!"; exit(0); }
Does Not Work:
eval {exit;}; if (my $e = $@) { # <--- secure... warn "Got the exception '$e'!"; exit(0); }

Replies are listed 'Best First'.
Re: exit inside an eval statement
by aukjan (Friar) on Jul 28, 2005 at 07:54 UTC
    from perldoc -f exit

    Don't use "exit" to abort a subroutine if there's any chancethat someone might want to trap whatever error happened. Use "die" instead, which can be trapped by an "eval".

    Hope this helps..
Re: exit inside an eval statement
by brian_d_foy (Abbot) on Jul 28, 2005 at 08:32 UTC

    eval{} does not trap exit(). When you tell the program to exit, that's what it does. It's not an error if it works right, and if it works right you're no longer running the script. :)

    --
    brian d foy <brian@stonehenge.com>
      Condición normal
Re: exit inside an eval statement
by anonymized user 468275 (Curate) on Jul 28, 2005 at 08:51 UTC
    There's a fair bit of work to do for this idea, but here it is 'on the back of an envelope':

    - overload the eval routine using exporter.

    - your own eval routine needs to substitute 'exit' with 'return'.

    - you therefore need yet another routine in a separate module which does not import your custom eval routine so that you can call it up to do the final (real) eval after the substitution has taken place.

    Update: All this of course assuming there is a reasonable reason for trapping the exit only if its in an eval.

    One world, one people