1. Perl
  2. Module
  3. here

Carp - Raise an exception from the caller's point of view

The Carp module allows you to raise an exception from the perspective of the module caller.

# Loading modules and importing functions
use Carp 'croak';

You can use the croak function to raise an exception from the perspective of the module caller. The caller's line number is added to the end of the message.

# Include the caller's line number in the error message to raise an exception
croak "Exception!";

Exceptions can be raised with the die function, which adds the line number of the executed position to the end of the message. croak differs from die in that it adds the calling line number to the end, not the line number where croak was executed.

Keep the following points in mind when using die and croak.

  • Use die to raise an exception in a Perl script (* .pl)
  • Use croak to raise an exception in a Perl module (* .pm).

What is the perspective of the module caller?

I think the term module caller's point of view is a little difficult to understand, so I'll explain it using an example.

Let's say your Module module defines a parse function. Suppose you call this function in the main package and you get an exception.

Let's first look at the die example.

# When an exception is raised with die # 1
package YourModule; # 2
                                          # 3
                                          # Four
sub parse {# 5
  # Raise an exception with die # 6
  die "Exception!"; # 7
# } 8
                                          # 9
package main; # 10
                                          # 11
                                          # 12
# Call the parse function. An exception occurs. # 13
YourModule::parse (); # 14

The program will exit with the following message: Contains the line number where the die was executed.

Exception! At a.pl line 7.

Next is the case of croak.

# When an exception is raised by croak # 1
package YourModule; # 2
use Carp 'croak'; # 3
                                          # Four
sub parse {# 5
  # Raise an exception with die # 6
  croak "Exception!"; # 7
# } 8
                                          # 9
package main; # 10
                                          # 11
                                          # 12
# Call the parse function. An exception occurs. # 13
YourModule::parse (); # 14

The program will then exit with the following message: Contains the line number where parse was called.

Exception! At a.pl line 14

You can use croak in this way to throw an exception from the caller's point of view.

See full stack trace

You may want to know all the information about where the function was called and where the exception occurred. In such cases, you can change the message added by croak to show the complete stack and race.

Just add the following line anywhere in the script.

# Change the croak message to one that contains the complete stack trace information
use Carp 'verbose';

I embedded it in the script above. I put it just before YourModule::parse ().

package main;

# Call the parse function
use Carp 'verbose';
YourModule::parse ();

Then you can see the complete stack trace as follows:

Exception! At a.pl line 7
        YourModule::parse () called at a.pl line 14

Generally speaking, many Perl modules on CPAN use croak to raise exceptions. This means that you can expect to see a complete stack trace by writing use Carp 'verbose'.

Also, if a module is utilizing yet another module, the exception raised there may not be the true caller's point of view.

Caller
▼
Module 1
▼
Module 2-If croak is executed here, the caller will be module 1.
              I don't know the line number of the true caller.

Even in such a case, you can get a complete stack trace by writing use Carp 'verbose'.

FAQ about Carp module

Q. The Carp module also has a function called confess, should I use croak?

A. confess can display the complete stack trace information, but croak seems to be preferred because the message is concise and you can switch to the complete stack trace information by specifying options.

Q. I want to raise a warning, not an exception.

A. You can use the carp function to raise a warning.

# warning
use Carp 'carp';
carp'Warning!';

Q. Is it possible to see the complete stack trace without changing the script?

A. Yes it is possible. If you want to specify it in script units, do as follows.

perl -MCarp = verbose script.pl

You can also use an environment variable called PERL5OPT to display a complete stack trace for all scripts. If PERL5OPT is already set to a value, make sure to connect it after the blank.

# bash
export PERL5OPT = -MCarp = verbose
export PERL5OPT = '-Mstrict -MCarp = verbose'

# Windows
set PERL5OPT = -MCarp = verbose
set PERL5OPT = -Mstrict -MCarp = verbose

Related Informatrion