NAME
FP::Carp - report to immediate caller
SYNOPSIS
use FP::Carp;
# Like `croak` but don't skip any parent call frames:
sub foo {
@_ == 1 or fp_croak "I need 1 argument";
}
# Easier if you just want to report an error about the number of
# arguments passed to the current subroutine:
sub bar {
@_ == 2 or fp_croak_arity 2;
}
sub test {
foo(@_);
bar(@_);
}
sub try(&) {
eval { &{$_[0]}; 1 } && return;
my $e= $@;
$e=~ s/\n.*//s;
$e=~ s{\\}{/}sg; # convert windows to unix paths
$e
}
is try { test(10) }, 'bar: needs 2 arguments (got 1) at lib/FP/Carp.pm line 31';
is try { test(10,11) }, 'I need 1 argument at lib/FP/Carp.pm line 30';
# there is currently no equivalent to `carp`, or `confess` (use
# Devel::Confess instead?)
DESCRIPTION
Carp skips call frames in the same package as the caller of `croak` (or `carp` etc.), as well as those from matching some other cases like parent classes. This works well when assuming that all the code that's being skipped is correct, and the error has to do with the code outside those scopes. This is also necessary when not using tail-call optimization (as via goto \&sub, or Sub::Call::Tail) to skip parent calls in tail position.
But for cases like checking the number of arguments to the current subroutine, this is not useful, as the error really is in the immediate caller. And if using tail-call optimization, just reporting the next frame is also correct in other cases when the current call is in the caller's tail position.
This provides Carp like subroutines for these cases--they go up exactly one frame, no matter what.
Since there's no logic for skipping of call frames, this module is simpler than Carp.
SEE ALSO
NOTE
This is alpha software! Read the status section in the package README or on the website.