Search⌘ K

Reporting Errors

Understand how to use Perl's caller built-in and the Carp module to report errors and warnings from the perspective of the caller. Learn to validate function parameters, check argument counts, and improve debugging by inspecting call contexts for more reliable Perl programming.

The caller built-in

We use the caller built-in to inspect a function’s calling context. When passed no arguments, caller returns a list containing the name of the calling package, the name of the file containing the call, and the line number of the file on which the call occurred:

Perl
package main;
main();
sub main {
show_call_information();
}
sub show_call_information {
my ($package, $file, $line) = caller();
say "Called from $package in $file:$line";
}

The full call chain is available for inspection. Pass a single-integer argument n to caller() to ...