next up previous contents
Next: Trapping Fatal Errors Up: Handling Errors and Signals Previous: Using the die() Function

Using the warn() Function

The warn() function has the same functionality that die() does except the script is not exited. This function is better suited for non-fatal messages like low memory or disk space conditions.

The next example tries to change to the /text directory. If the connect fails, the consequences are not fatal because the files can still be written to the current directory.

chdir('/text') ||
   warn("Using current directory instead of /text, warning");

This line of code displays

Using current directory instead of /text, warning at test.pl line 2.

if the /text directory does not exist. As with die(), you can eliminate the script name and line number by ending your error message with a newline. You could also use the $! variable to display the system error message.



dave@cs.cf.ac.uk