exit
exit EXPR
exitThis function evaluates EXPR as an
integer and exits immediately with that value as the final error status
of the program. If EXPR is omitted, the
function exits with 0 status (meaning
“no error”). Here’s a fragment that lets a user exit the program by
typing x or X:
$ans = <STDIN>; exit if $ans =~ /^[Xx]/;
You shouldn’t use exit to abort
a subroutine if there’s any chance that someone might want to trap
whatever error happened. Use die
instead, which can be trapped by an eval. Or use one of die’s wrappers from the Carp module, like croak or
confess.
We said that the exit function
exits immediately, but that was a bald-faced lie. It exits as soon as
possible, but first it calls any defined END routines for at-exit handling. These
routines cannot abort the exit, although they can change the eventual
exit value by setting the $?
variable. Likewise, any class that defines a DESTROY method will invoke that method on
behalf of all its objects before the real program exits. If you really
need to bypass exit processing, you can call the POSIX module’s _exit
function to avoid all END and
destructor processing. And if POSIX
isn’t available, you can exec
"/bin/false" or some such.
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access