Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

perl

How to handle errors in Perl

Muhammad Ashir

Perl provides a number of functions and utilities for error reporting and handling. Let’s have a look at some of the methods and functions used for error handling.

Built-in methods

Perl provides two built-in methods for error reporting, the die() and the warn() functions.

Die()

Die raises an exception, the uncaught exception is printed to STDERR, and the program execution is terminated.

Die is often used with a string that is raised as an exception. The string is user-defined and can contain whatever message the user wants.

open FileHandle "test.txt" or die "Failed to open file $!\n"

The $! variable is a special variable that holds the error returned by the system. We can print it out for a more detailed description of the error.

Warn()

The warn() function raises an exception and prints it out to STDERR, but does not stop the execution of the program.

open FileHandle "test.txt" or warn "Failed to open file $!\n"

Errors in modules

Error handling when dealing with modules is a little more tricky. If there is an error in a module, it is important to let the programmer who is using the module know about the error.

The two situations that we should be able to handle are, reporting the error that tells the filename and line number in the module and reporting an error that quotes the caller’s information.

Suppose you have a module named errorModule that has a function and throws an exception. This is how we would look on the output.

package errorModule;

sub someFunction(){
    warn "This module has an error";
}

someFunction();
use errorModule;
someFunction();

Output

This module has an error at errorModule.pm line 4

However, sometimes the programmer may need information about who the caller is instead of where the error occurred within the module. The Carp module comes in handy here.

The Carp module

The Carp module is similar to warn and die but for modules. It provides four main functions:

  • carp
  • cluck
  • croak
  • confess

carp()

The carp function is similar to the warn function. It displays the warning message at the module line and module filename.

package errorModule;
use Carp;

sub someFunction(){
  carp "This Module has an error!";
}
use errorModule;
someFunction();

Output

This module has an error! at errorModule.pm line 4

Cluck()

The cluck() function produces the whole stack trail of every call in the call stack. It prints out the same error message as the carp() function, but it also shows a list of all the calls that lead to the error.

package errorModule;
use Carp qw(cluck);

sub someFunction(){
  cluck "This Module has an error!";
}
use errorModule;
someFunction();

Output

This module has an error! at errorModule.pm line 4 errorModule::someFunction() called at main.pl line 2

Croak()

The croak() function is similar to the die function. It prints out the module name and line at which the error occurs, but does not provide any information about the call stack. It also stops program execution.

package errorModule;
use Carp;

sub someFunction(){
  croak "This Module has an error!";
}
use errorModule;
someFunction();

Output

This module has an error! at errorModule.pm line 4

Confess()

The confess() function, like croak(), prints out the complete information about the call stack and terminates program execution.

package errorModule;
use Carp;

sub someFunction(){
  croak "This Module has an error!";
}
use errorModule;
someFunction();

Output

This module has an error! at errorModule.pm line 5 Test::someFunction() called at main.pl line 2

RELATED TAGS

perl

CONTRIBUTOR

Muhammad Ashir
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring