Search code examples
perl

Can I generate a stack trace for a warning in Perl?


I'm writing a library that uses many modules that call Net::Http::Methods::format_request, unfortunately during the execution of code in my library I'm getting a warning:

Use of uninitialized value $v in concatenation (.) or string at ~/perl5/lib/perl5/Net/HTTP/Methods.pm line 167.

Is there any way for me to get a stack-trace from this warning so I can narrow it down to the exact call? My first thought is to add no warnings above each potential call, but that seems a impractical given the number of calls. (Though I will do it if I have to).


Solution

  • Use

    PERL5OPT=-MCarp::Always script.pl
    

    or

    perl -MCarp::Always script.pl
    

    Alternatively, you could use the debugger.

    Load the program (perl -d script.pl), set a breakpoint, and run it (r). Setting the breakpoint would look like this:

    b /home/username/perl5/lib/perl5/Net/HTTP/Methods.pm:167 !defined( $v )
    

    Or add the following before the offending line

    $DB::single = 1 if !defined( $v );
    

    Then, load the program in the Perl debugger (perl -d script.pl), and run it (r).

    Either way, T will provide a stack trace once it stops at the breakpoint.