die Statements

Oh, and that or die... thing. It’s a really, really good idea to stick an or die... statement like this in your Perl script every time the script is trying to open a filehandle or execute an external command of some sort. You should do this because these sorts of actions represent weak links in your script, where something can easily go wrong even though the script itself is functioning properly. Let’s say you get your script working and everything’s going great, but then someone comes along and moves the sendmail program on your Unix server, such that Perl can’t find it when it tries to open this pipe. Without an or die... statement, Perl will try to open the pipe, fail, and continue on as if nothing had happened. You, and the users of your CGI scripts, will never know there’s something wrong—except that you won’t be getting any emailed form submissions.

With the or die... statement there, however, the script will try to open the pipe, and when it fails the script will execute whatever comes after the or, which in this case means the die statement. The die statement makes the script stop dead in its tracks and print an error message. That message is printed to a special place called standard error , which actually means it prints to your screen in the case of scripts you run from the command line, or to the web server’s error log in the case of a CGI script. The error message that gets printed there is whatever comes after the die statement, so by making it a ...

Get Perl for Web Site Management now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.