Error Handling in PERL

previous next AddThis Social Bookmark Button

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.

Using if

The if statement is the obvious choice when you need to check the return value from a statement; for example:

if (open(DATA,$file))
   die "Error: Couldn't open the file $!";

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:

die "Error: Something went wrong\n" if (error());

Using unless

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:

   die "Error: Can't change directory!: $!";

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:

die "Error: Can't change directory!: $!" unless(chdir("/etc"));

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:

print(exists($hash{value}) ? 'There' : 'Missing',"\n");

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.

chdir('/etc') or warn "Can't change directory";

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.

chdir('/etc') or die "Can't change directory";

Reporting Errors Within Modules

There are two different situations we need to be able to handle:

  • Reporting an error in a module that quotes the module's filename and line number - this is useful when debugging a module, or when you specifically want to raise a module-related, rather than script-related, error.

  • Reporting an error within a module that quotes the caller's information so that you can debug the line within the script that caused the error. Errors raised in this fashion are useful to the end-user, because they highlight the error in relation to the calling script's origination line.

The warn and die functions work slightly differently than you would expect when called from within a module. For example, the simple module:

package T;

require Exporter;
@ISA = qw/Exporter/;
@EXPORT = qw/function/;
use Carp;

sub function
   warn "Error in module!";

when called from a script

use T;

It will produce following result
Error in module! at line 11.

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.

carp "Error in module!";

This would result in
Error in module! at line 3

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.

cluck "Error in module!";

This would result in something like
Error in module! at line 11
    T::function() called at line 13
    S::raise() called at line 3

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:

croak "Definitely didn't work";

This would result in
Error in module! at line 13

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.

confess "Failed around about there";

This would result in
Error in module! at line 11
   T::function() called at line 13
   S::raise() called at line 3

previous next Printer Friendly