Fatal Errors with die
Let’s step aside for a moment. We need some stuff that isn’t directly related to (or limited to) I/O but is more about getting out of a program earlier than normal.
When a fatal error happens inside Perl (for example, if you divide by zero, use an invalid regular expression, or call a subroutine that hasn’t been declared), your program stops with an error message telling why. This functionality is available to us with the
die function, so we can make our own fatal errors.
die function prints out the message you give it (to the standard error stream, where such messages should go) and ensures that your program exits with a nonzero exit status.
You may not know it, but every program that runs on Unix (and many other modern operating systems) has an exit status, telling if it was successful. Programs that run other programs (like the make utility program) look at that exit status to see that everything happened correctly. The exit status is a single byte, so it can’t say much; traditionally, it is zero for success and a nonzero value for failure. Perhaps one means a syntax error in the command arguments, two means that something went wrong during processing, and three means the configuration file couldn’t be found; the details differ from one command to the next. But zero always means that everything worked. When the exit status shows failure, a program like make knows not to go on to the next step.
We could rewrite the previous example, perhaps with something ...