in reply to Re^2: Exiting script from subs
in thread Exiting script from subs

What the Camel does or doesn't say is mostly irrelevant. If your coding standards allow for exit() within a subroutine, then use it. However, most of the community has come to expect die() within subroutines and exit() (if it's even used) from the main scope.

Now, die() and exit() do different things. The biggest difference is

eval { print "Inside\n"; die; }; print "Outside\n"; ---- Inside Outside
vs.
eval { print "Inside\n"; exit; }; print "Outside\n"; ---- Inside
exit() exits, end of story. die() allows for some manner of error trapping. This is the main reason why die() is preferred within subroutines. You may want to reuse that subroutine somewhere else that wants to be able to trap the abnormal termination. Also, die() triggers $SIG{DIE}, which can also be useful to trap.

Replies are listed 'Best First'.
Re^4: Exiting script from subs
by gam3 (Curate) on Apr 16, 2005 at 03:10 UTC
    You can trap an exit (termination of a script) as well.
    END { print "Outside"; } eval { print "Inside\n"; exit; }; print "Outside\n"; ---- Inside Outside
    -- gam3
    A picture is worth a thousand words, but takes 200K.
      That's not trapping an exit. That's trapping the termination of the script. Remove your eval and change your exit to a die and see what happens.