You can identify and trap an error in a number of different ways. Its very easy to trap errors in Perl and then handling them properly. Here are few methods which can be used.
The if statement is the obvious choice when you need to check the return value from a statement; for example:
Here variable $! returns the actual error message
Alternatively, we can reduce the statement to one line in situations where it makes sense to do so; for example:
The unless function is the logical opposite to if: statements can completely bypass the success status and only be executed if the expression returns false. For example:
The unless statement is best used when you want to raise an error or alternative only if the expression fails. The statement also makes sense when used in a single-line statement:
Here we die only if the chdir operation fails, and it reads nicely.
Using the Conditional Operator
For very short tests, you can use the conditional operator:
It's not quite so clear here what we are trying to achieve, but the effect is the same as using an if or unless statement. The conditional operator is best used when you want to quickly return one of two values within an expression or statement.
The Warn Function
The warn function just raises a warning, a message is printed to STDERR, but no further action is taken.
The Die Function
The die function works just like warn, except that it also calls exit. Within a normal script, this function has the effect of immediately terminating execution.
Reporting Errors Within Modules
There are two different situations we need to be able to handle:
The warn and die functions work slightly differently than you would expect when called from within a module. For example, the simple module:
when called from a script
This is more or less what you might expect, but not necessarily what you want. From a module programmer's perspective, the information is useful because it helps to point to a bug within the module itself. For an end-user, the information provided is fairly useless, and for all but the hardened programmer, it completely pointless.
The solution for such problems is the Carp module, which provides a simplified method for reporting errors within modules that return information about the calling script. The Carp module provides four functions: carp, cluck, croak, and confess. These functions are discussed below
The Carp Function
The carp function is the basic equivalent of warn and prints the message to STDERR without actually exiting the script and printing the script name.
The Cluck Function
The cluck function is a sort of supercharged carp, it follows the same basic principle but also prints a stack trace of all the modules that led to the function being called, including information on the original script.
The Croak Function
The croak function is the equivalent of die, except that it reports the caller one level up. Like die, this function also exits the script after reporting the error to STDERR:
As with carp, the same basic rules apply regarding the including of line and file information according to the warn and die functions.
The Confess Function
The confess function is like cluck; it calls die and then prints a stack trace all the way up to the origination script.