LanX has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I'm getting counter intuitive results

From the docs of to Carp#$Carp::Verbose

my results show no difference...

What am I missing?

-*- mode: compilation; default-directory: "d:/tmp/job/" -*- Compilation started at Thu Sep 30 16:29:14 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/job/test_carp.pl Perl-Version 5.032001 MSWin32S ====== default $Carp::Verbose =0 === Testing die Fehlermeldung at d:/tmp/job/test_carp.pl line 24. === Testing croak Fehlermeldung at d:/tmp/job/test_carp.pl line 22. main::throw0("croak") called at d:/tmp/job/test_carp.pl line 31 main::throw1("croak") called at d:/tmp/job/test_carp.pl line 35 main::throw2("croak") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 === Testing confess Fehlermeldung at d:/tmp/job/test_carp.pl line 26. main::throw0("confess") called at d:/tmp/job/test_carp.pl line 31 main::throw1("confess") called at d:/tmp/job/test_carp.pl line 35 main::throw2("confess") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 ====== Setting Verbose to 0 === Testing die Fehlermeldung at d:/tmp/job/test_carp.pl line 24. === Testing croak Fehlermeldung at d:/tmp/job/test_carp.pl line 22. main::throw0("croak") called at d:/tmp/job/test_carp.pl line 31 main::throw1("croak") called at d:/tmp/job/test_carp.pl line 35 main::throw2("croak") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 === Testing confess Fehlermeldung at d:/tmp/job/test_carp.pl line 26. main::throw0("confess") called at d:/tmp/job/test_carp.pl line 31 main::throw1("confess") called at d:/tmp/job/test_carp.pl line 35 main::throw2("confess") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 ====== Setting Verbose to 1 === Testing die Fehlermeldung at d:/tmp/job/test_carp.pl line 24. === Testing croak Fehlermeldung at d:/tmp/job/test_carp.pl line 22. main::throw0("croak") called at d:/tmp/job/test_carp.pl line 31 main::throw1("croak") called at d:/tmp/job/test_carp.pl line 35 main::throw2("croak") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 === Testing confess Fehlermeldung at d:/tmp/job/test_carp.pl line 26. main::throw0("confess") called at d:/tmp/job/test_carp.pl line 31 main::throw1("confess") called at d:/tmp/job/test_carp.pl line 35 main::throw2("confess") called at d:/tmp/job/test_carp.pl line 48 eval {...} called at d:/tmp/job/test_carp.pl line 48 Compilation finished at Thu Sep 30 16:29:14

here my code

use strict; use warnings; use Carp; #use Data::Dumper; sub out {warn @_,"\n" } out "Perl-Version $] $^OS"; out '====== default $Carp::Verbose =',$Carp::Verbose; my $err_msg = "Fehlermeldung"; sub throw0 { my ($type) = @_; out "=== Testing $type"; 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(@_) } for my $verbose ( undef,0,1 ){ if (defined $verbose) { $Carp::Verbose = $verbose; out "====== Setting Verbose to $verbose"; } # --- types of dieing for my $type (qw/die croak confess/){ eval { throw2($type) } or out $@; } } #done_testing();

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery

update

I think I found the issue

Replies are listed 'Best First'.
Re: $Carp::Verbose ?
by roboticus (Chancellor) on Sep 30, 2021 at 15:24 UTC

    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.

      Thanks, solved it.

      It depends on the callers package ... and I have to admit we had this discussion before.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

Re: $Carp::Verbose ? (SOLVED)
by LanX (Saint) on Sep 30, 2021 at 15:41 UTC
    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

    use strict; use warnings; package SAME; use Carp; use Data::Dumper; sub out {warn @_,"\n" } out "Perl-Version $] $^OS"; out '====== default $Carp::Verbose =', Dumper $Carp::Verbose; my $err_msg = "Fehlermeldung"; sub throw0 { my ($type) = @_; out "=== Testing $type"; 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(@_) } my $PACKAGE="OTHER"; 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 (qw/die croak confess/) { eval <<"__CODE__" or out $@; package $PACKAGE; use Data::Dumper; sub client0 { SAME::throw2(\@_); }; sub client1 { client0(\@_); }; client1("$type"); __CODE__ } }

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery

      here a shorter test, avoiding string evals and concentrating on croak.

      (line 72 is inside sub client0)

      You can uncomment other cases if needed.

      -*- mode: compilation; default-directory: "d:/tmp/job/" -*- Compilation started at Thu Sep 30 19:14:48 C:/Strawberry/perl/bin\perl.exe -w d:/tmp/job/test_carp.pl xxxxxxxxx Perl-Version 5.032001 MSWin32S ********* Setting Verbose to 0 ====== Testing <croak> ... from SAME package error-messageby <croak> from SAME package at d:/tmp/job/test_carp.pl l +ine 54. SAME::throw0("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 63 SAME::throw1("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 67 SAME::throw2("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 82 SAME::client0("SAME", "croak") called at d:/tmp/job/test_carp.pl l +ine 85 SAME::client1("SAME", "croak") called at d:/tmp/job/test_carp.pl l +ine 42 eval {...} called at d:/tmp/job/test_carp.pl line 42 ... from OTHER package error-messageby <croak> from OTHER package at d:/tmp/job/test_carp.pl +line 74. ... from THIRD package error-messageby <croak> from THIRD package at d:/tmp/job/test_carp.pl +line 74. ********* Setting Verbose to 1 ====== Testing <croak> ... from SAME package error-messageby <croak> from SAME package at d:/tmp/job/test_carp.pl l +ine 54. SAME::throw0("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 63 SAME::throw1("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 67 SAME::throw2("SAME", "croak") called at d:/tmp/job/test_carp.pl li +ne 82 SAME::client0("SAME", "croak") called at d:/tmp/job/test_carp.pl l +ine 85 SAME::client1("SAME", "croak") called at d:/tmp/job/test_carp.pl l +ine 42 eval {...} called at d:/tmp/job/test_carp.pl line 42 ... from OTHER package error-messageby <croak> from OTHER package at d:/tmp/job/test_carp.pl +line 54. SAME::throw0("OTHER", "croak") called at d:/tmp/job/test_carp.pl l +ine 63 SAME::throw1("OTHER", "croak") called at d:/tmp/job/test_carp.pl l +ine 67 SAME::throw2("OTHER", "croak") called at d:/tmp/job/test_carp.pl l +ine 74 OTHER::client0("OTHER", "croak") called at d:/tmp/job/test_carp.pl + line 77 OTHER::client1("OTHER", "croak") called at d:/tmp/job/test_carp.pl + line 42 eval {...} called at d:/tmp/job/test_carp.pl line 42 ... from THIRD package error-messageby <croak> from THIRD package at d:/tmp/job/test_carp.pl +line 54. SAME::throw0("THIRD", "croak") called at d:/tmp/job/test_carp.pl l +ine 63 SAME::throw1("THIRD", "croak") called at d:/tmp/job/test_carp.pl l +ine 67 SAME::throw2("THIRD", "croak") called at d:/tmp/job/test_carp.pl l +ine 74 OTHER::client0("THIRD", "croak") called at d:/tmp/job/test_carp.pl + line 90 THIRD::client0("THIRD", "croak") called at d:/tmp/job/test_carp.pl + line 93 THIRD::client1("THIRD", "croak") called at d:/tmp/job/test_carp.pl + line 42 eval {...} called at d:/tmp/job/test_carp.pl line 42 Compilation finished at Thu Sep 30 19:14:48

      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(@_); };

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery