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

I have to admit that I misunderstood the way Carp works, but I really struggle with the documentation here.

The synopsis says

# warn user (from perspective of caller) carp "string trimmed to 80 chars";

... while it's later corrected to ...

   report the error as being from where your module was called.

(a behavior I actually prefer!)

More importantly it says

   longmess( "message from cluck() or confess()" );

... but as you can see from the following example the first (i.e. current) line from cluck is omitted.

What am I missing?

use strict; use warnings; TEST::first(1); # 4 package TEST; use Carp qw/carp cluck longmess shortmess/; sub first { second(2); # 10 } sub second { # local $Carp::Verbose=1; carp "Carp Second"; cluck "Cluck Second"; # 16 print shortmess("Shortmess Second"); print longmess("Longmess Second"); }

Carp Second at d:/Users/lanx/pm/carp.pl line 4. Cluck Second at d:/Users/lanx/pm/carp.pl line 16. TEST::second(2) called at d:/Users/lanx/pm/carp.pl line 10 TEST::first(1) called at d:/Users/lanx/pm/carp.pl line 4 Shortmess Second at d:/Users/lanx/pm/carp.pl line 4. Longmess Second at d:/Users/lanx/pm/carp.pl line 10. TEST::first(1) called at d:/Users/lanx/pm/carp.pl line 4

update

Ironically after activating the $Carp::Verbose flag, shortmess becomes longer than longmess .

Shortmess Second at d:/Users/lanx/pm/carp.pl line 17. TEST::second(2) called at d:/Users/lanx/pm/carp.pl line 10 TEST::first(1) called at d:/Users/lanx/pm/carp.pl line 4 Longmess Second at d:/Users/lanx/pm/carp.pl line 10. TEST::first(1) called at d:/Users/lanx/pm/carp.pl line 4

wot???

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

update

translated the test to a proper module.pm in a separate file with the same result.

Replies are listed 'Best First'.
Re: Carp::longmess not reporting the current line?
by LanX (Saint) on Jun 01, 2018 at 19:18 UTC
    After looking into the source I discovered this

    sub longmess { local($!, $^E); # Icky backwards compatibility wrapper. :-( # # The story is that the original implementation hard-coded the # number of call levels to go back, so calls to longmess were off # by one. Other code began calling longmess and expecting this # behaviour, so the replacement has to emulate that behaviour. my $cgc = _cgc(); my $call_pack = $cgc ? $cgc->() : caller(); if ( $Internal{$call_pack} or $CarpInternal{$call_pack} ) { return longmess_heavy(@_); } else { local $CarpLevel = $CarpLevel + 1; return longmess_heavy(@_); } }

    "of by one" :(

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

      Holy cow - backward compatibility! We're trapped in the past, and will die from that - it will pull us into oblivion.

      perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
        Backwards compatibility is fine as long as it's documented and a saner alternative is available.

        Unfortunately does Carp also not support error objects...

        ... but at least that's documented.

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