Rolf:
I just tried it out and can confirm: for the given code, I get the same result no matter the setting of $Carp::Verbose--even if I run it as "perl -MCarp=verbose pm11137139.pl".
...roboticus
When your only tool is a hammer, all problems look like your thumb.
| [reply] [d/l] |
| [reply] |
It's not the first time I'm stumbling over the documentation for Carp and it's still confusing me every time...
Point is that carp and croak are meant for reporting module errors but from the perspective of a client who is using a module.
This is decided by checking the package of the subs in the call stack.
BUT if they all belong to the same package they will behave like confess and give the full stacktrace
Here is a more complete description of how carp and croak work. What they do is search the call-stack for a function call stack where they have not been told that there shouldn't be an error. If every call is marked safe, they give up and give a full stack backtrace instead. In other words they presume that the first likely looking potential suspect is guilty.
The following example shows what happens if the call happens from an OTHER package instead of the SAME.
-*- mode: compilation; default-directory: "d:/tmp/job/" -*-
Compilation started at Thu Sep 30 17:33:06
C:/Strawberry/perl/bin\perl.exe -w d:/tmp/job/test_carp.pl
Perl-Version 5.032001 MSWin32S
====== default $Carp::Verbose =$VAR1 = 0;
====== Setting Verbose to 0
=== Testing die
Fehlermeldung at d:/tmp/job/test_carp.pl line 27.
=== Testing croak
Fehlermeldung at (eval 6) line 4.
=== Testing confess
Fehlermeldung at d:/tmp/job/test_carp.pl line 29.
SAME::throw0("confess") called at d:/tmp/job/test_carp.pl line 34
SAME::throw1("confess") called at d:/tmp/job/test_carp.pl line 38
SAME::throw2("confess") called at (eval 7) line 4
OTHER::client0("confess") called at (eval 7) line 7
OTHER::client1("confess") called at (eval 7) line 9
eval ' package OTHER;
use Data::Dumper;
sub client0 {
SAME::throw2(@_);
};
sub client1 {
client0(@_);
};
client1("confess");
' called at d:/tmp/job/test_carp.pl line 53
====== Setting Verbose to 1
=== Testing die
Fehlermeldung at d:/tmp/job/test_carp.pl line 27.
=== Testing croak
Fehlermeldung at d:/tmp/job/test_carp.pl line 25.
SAME::throw0("croak") called at d:/tmp/job/test_carp.pl line 34
SAME::throw1("croak") called at d:/tmp/job/test_carp.pl line 38
SAME::throw2("croak") called at (eval 9) line 4
OTHER::client0("croak") called at (eval 9) line 7
OTHER::client1("croak") called at (eval 9) line 9
eval ' package OTHER;
use Data::Dumper;
sub client0 {
SAME::throw2(@_);
};
sub client1 {
client0(@_);
};
client1("croak");
' called at d:/tmp/job/test_carp.pl line 53
=== Testing confess
Fehlermeldung at d:/tmp/job/test_carp.pl line 29.
SAME::throw0("confess") called at d:/tmp/job/test_carp.pl line 34
SAME::throw1("confess") called at d:/tmp/job/test_carp.pl line 38
SAME::throw2("confess") called at (eval 10) line 4
OTHER::client0("confess") called at (eval 10) line 7
OTHER::client1("confess") called at (eval 10) line 9
eval ' package OTHER;
use Data::Dumper;
sub client0 {
SAME::throw2(@_);
};
sub client1 {
client0(@_);
};
client1("confess");
' called at d:/tmp/job/test_carp.pl line 53
Compilation finished at Thu Sep 30 17:33:06
here the code
| [reply] [d/l] [select] |
use strict;
use warnings;
package SAME;
use Carp;
use Data::Dumper;
sub out {
$_[0] =~ m/^([-.*=]+ )/;
my $nl = "\n" x ( length($1//0)/3);
warn $nl, @_,"\n"
}
out "xxxxxxxxx Perl-Version $] $^OS";
#out '========= default $Carp::Verbose = ',$Carp::Verbose;
my $err_msg = "error-message";
for my $verbose (
0,
1,
) {
if (defined $verbose) {
$Carp::Verbose = $verbose;
out "********* Setting Verbose to $verbose";
}
no warnings 'redefine';
# --- types of dieing
for my $type (
# "die",
"croak",
# "confess",
) {
out "====== Testing <$type>";
for my $PACKAGE (qw/SAME OTHER THIRD/) {
out "... from $PACKAGE package";
eval { $PACKAGE->client1($type) } or out $@;
}
}
}
sub throw0 {
my ($pkg,$type) = @_;
my $err_msg = $err_msg . "by <$type> from $pkg package";
eval { goto $type } or die "Testcase '$type' not implemented";
croak:
croak $err_msg;
die:
die $err_msg;
confess:
confess $err_msg;
}
sub throw1 {
throw0(@_)
}
sub throw2 {
throw1(@_)
}
package OTHER;
sub client0 {
SAME::throw2(@_);
};
sub client1 {
client0(@_);
};
package SAME;
sub client0 {
SAME::throw2(@_);
};
sub client1 {
client0(@_);
};
package THIRD;
sub client0 {
OTHER::client0(@_);
};
sub client1 {
client0(@_);
};
| [reply] [d/l] [select] |