Programming Perl

Programming PerlSearch this book
Previous: 7.2.4 Benchmark - Check and Compare Running Times of CodeChapter 7
The Standard Perl Library
Next: 7.2.6 Config - Access Perl Configuration Information
 

7.2.5 Carp - Generate Error Messages

use Carp;
carp "Be careful!";         # warn of errors (from perspective of caller)
croak "We're outta here!";  # die of errors (from perspective of caller)
confess "Bye!";             # die of errors with stack backtrace

carp() and croak() behave like warn and die, respectively, except that they report the error as occurring not at the line of code where they are invoked, but at a line in one of the calling routines. Suppose, for example, that you have a routine goo() containing an invocation of carp(). In that case - and assuming that the current stack shows no callers from a package other than the current one - carp() will report the error as occurring where goo() was called. If, on the other hand, callers from different packages are found on the stack, then the error is reported as occurring in the package immediately preceding the package in which the carp() invocation occurs. The intent is to let library modules act a little more like built-in functions, which always report errors where you call them from.

confess() is like die except that it prints out a stack backtrace. The error is reported at the line where confess() is invoked, not at a line in one of the calling routines.


Previous: 7.2.4 Benchmark - Check and Compare Running Times of CodeProgramming PerlNext: 7.2.6 Config - Access Perl Configuration Information
7.2.4 Benchmark - Check and Compare Running Times of CodeBook Index7.2.6 Config - Access Perl Configuration Information