The Carp Module

It is alternative to warn and die for modules. It is useful for user-defined modules because they act somewhat like die() and warn(), but with a message which is added by the programmer.
This Carp module provides some functions which are:

  1. carp()
  2. cluck()
  3. croak()
  4. confess()

carp() function: This function works similar to warn function and prints the message given by the user without exiting the script.
Example:

perl




package test;
use Carp;
  
sub functionName
{
   carp "Error in module!"
}
1;


use test;
functionName();

Output:

Error in module!  at test.pm line 8

cluck() function: This function produces a context which is a summary of every call in the call-stack. It follows the same process as the carp() function does but it prints a stack of all the modules that led to the call to the specific function.
Example:

perl




package Test;
use Carp qw(cluck);
  
sub function_name {
    cluck "Error in module!";
}
1;


use Test;
function_name();

Output:

Error in module! at Test.pm line 5
   Test::function_name() called at test.pl line 2

croak() function: This function is similar to die() function, except that it produces a shorter message which reports the error as being from where your module was called.
Example:

perl




package Test;
use carp;
  
sub functionName {
    croak "Error in module!";
}
1;


use Test;
functionName();

Output:

Error in module! at test.pl line 2

confess() function: This function is similar to cluck() function. It calls the die function and then prints a stack of all the modules that led to the call to the specific function.
Example:

perl




package Test;
use Carp;
  
sub functionName {
    confess "Error in module!";
}
1;


use Test;
functionName();

Output:

Error in module! at Test.pm line 5
   Test::functionName() called at test.pl line 2


Error Handling in Perl

Error Handling in Perl is the process of taking appropriate action against a program that causes difficulty in execution because of some error in the code or the compiler. Processes are prone to errors. For example, if opening a file that does not exist raises an error, or accessing a variable that has not been declared raises an error.
The program will halt if an error occurs, and thus using error handling we can take appropriate action instead of halting a program entirely. Error Handling is a major requirement for any language that is prone to errors.

Errors can be classified by the time at which they occur:

  1. Compile Time Errors
  2. Run Time Errors

Compile Time Error: It is an error such as syntax error or missing file reference that prevents the program from successfully compiling.

Run Time Error : It is an error that occurs when the program is running and these errors are usually logical errors that produce the incorrect output.

Similar Reads

Error Handling in Perl

Perl provides two builtin functions to generate fatal exceptions and warnings, that are:...

Error within Modules

...

The Carp Module

...

Contact Us