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

Hi

Anything wrong with this approach to conditionally force a stacktrace with legacy code ?

I know that there is Carp::Always , but it warns that it's manipulating the SIG-handlers and doesn't play well along other modules doing so.

use strict; use warnings; use Carp; use constant ERR_BACKTRACE => 1; BEGIN { if (ERR_BACKTRACE) { *CORE::GLOBAL::warn = \&Carp::cluck; *CORE::GLOBAL::die = \&Carp::confess; } } sub t2{ warn "warn with trace"; } sub t1 { t2(21,22,23); die "die with trace"; } t1(11,12,13);

-->

Compilation started at Fri Sep 27 15:18:09 C:/Perl_524/bin\perl.exe d:/exp/t_carp.pl warn with trace at d:/exp/t_carp.pl line 17. main::t2(21, 22, 23) called at d:/exp/t_carp.pl line 21 main::t1(11, 12, 13) called at d:/exp/t_carp.pl line 25 die with trace at d:/exp/t_carp.pl line 22. main::t1(11, 12, 13) called at d:/exp/t_carp.pl line 25 Compilation exited abnormally with code 255 at Fri Sep 27 15:18:10

Cheers Rolf
(addicted to the Perl Programming Language :)
Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

Replies are listed 'Best First'.
Re: Forcing stacktrace, alternative to Carp::Always
by tobyink (Canon) on Sep 27, 2019 at 13:53 UTC

    It won't catch errors and warnings thrown by built-ins because they don't call CORE::warn or CORE::die.

    require "this-file-does-not-exist"; # no stack trace
Re: Forcing stacktrace, alternative to Carp::Always
by jcb (Parson) on Sep 27, 2019 at 21:41 UTC

    How does the debugger do it? Could you replicate that?

      I suppose by installing SIG handlers too, with the same potential problems.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        > I suppose by installing SIG handlers too, with the same potential problems.

        just verified it. From perl5db.pl

        $SIG{__WARN__} = \&DB::dbwarn;

        and DB::dbwarn() is internally using Carp::longmess()

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        Wikisyntax for the Monastery FootballPerl is like chess, only without the dice