12.12. Reporting Errors and Warnings Like Built-InsProblemYou want to generate errors and warnings in your modules, but when you use SolutionThe standard Carp module provides functions to do this. Use DiscussionLike built-ins, some of your module's functions generate warnings or errors if all doesn't go well. Think about sub even_only {
my $n = shift;
die "$n is not even" if $n & 1; # one way to test
#....
}then the message will say it's coming from the file your use Carp;
sub even_only {
my $n = shift;
croak "$n is not even" if $n % 2; # here's another
#....
}If you just want to complain about something, but have the message report where in the user's code the problem occurred, call use Carp;
sub even_only {
my $n = shift;
if ($n & 1) { # test whether odd number
carp "$n is not even, continuing";
++$n;
}
#....
}Many built-ins emit warnings only when the -w command-line switch has been used. The carp "$n is not even, continuing" if $^W; Finally, the Carp module provides a third function: See AlsoThe |