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

Hi monks,

I was wondering if there's a way to configure the Carp stack backtrace, as printed by longmess or confess, to get a shorter line. For instance, with just the filename + line number, and a shorter, relative path:

Big error thrown. Stack: ./lib/Foo.pm:15 ./lib/Bar.pm:320 ./lib/Base/BarBase.pm:210

Well, you get the drill.

cheers!

andrea

Replies are listed 'Best First'.
Re: Shorter Carp stack backtrace
by almut (Canon) on Apr 16, 2010 at 01:34 UTC

    I don't think this can be configured — at least not without modifying the source :)

    From Carp/Heavy.pm:

    sub ret_backtrace { ... my %i = caller_info($i); $mess = "$err at $i{file} line $i{line}$tid_msg\n"; while (my %i = caller_info(++$i)) { $mess .= "\t$i{sub_name} called at $i{file} line $i{line}$tid_ms +g\n"; } return $mess; }
      I agree. Strangely, it won't let me monkeypatch it, such as in:

      sub Carp::Heavy::ret_backtrace{ return 'short...' }

      Don't know why, but it calls its old code. It let's me patch Carp::shortmess though.

      cheers,

      andrea

      $Carp::CarpLevel?

        How would that change the line format of the backtrace to file:line, as desired?

        #!/usr/bin/perl use Carp 'cluck'; sub foo { bar() } sub bar { baz() } sub baz { cluck "big error!" } foo(); $Carp::CarpLevel = 2; foo(); __END__ big error! at ./834994.pl line 7 main::baz() called at ./834994.pl line 6 main::bar() called at ./834994.pl line 5 main::foo() called at ./834994.pl line 9 big error! at ./834994.pl line 5 main::foo() called at ./834994.pl line 13