7.10 A Note About die()


7.10 A Note About die()

In Chapter 4, we learned that when things go disastrously wrong in a Perl program, the situation should be dealt with by terminating the program with the die() function. This function prints a message to the standard error output, cleans up nicely , and exits the program with a nonzero exit status.

In CGI programs, if the die() function is executed, we may think that the string passed to the function will end up in the browswer. However, standard error output is not sent on to the client that requested the CGI program; instead, it is written to the error log file (here, /var/www/logs/error_log ). For instance, if the CGI program test.cgi contains this line:

 open FH, /var/www/data/mydata.dat or die "File not opened: $!"; 

Let's say that the file /var/www/data/mydata.dat doesn't exist. When the client tries to open it, the open() fails, causing the die() function to be executed. The die() generates text that resembles the following:

 File not opened: No such file or directory at /var/www/cgi-bin/test.cgi  line 10. 

At first glance, one would think that this output would be sent on to the client, informing it of our problem. However, this is not true. A quick check of the log file would show this output was sent there.

So if you want to handle error conditions cleanly with CGI programs, don't use die() to do it. Instead, consider rolling your own error handler, as we do in the following project.



Open Source Development with Lamp
Open Source Development with LAMP: Using Linux, Apache, MySQL, Perl, and PHP
ISBN: 020177061X
EAN: 2147483647
Year: 2002
Pages: 136

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net