Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Here is my debugger tip.

When I do rapid prototyping, I often want the debugger to stop whenever a warning is issued from the Perl interpreter (for example about an undefined scalar). The warning contains a line number which is fine, but when executed in a loop, this is not enough. I need the current context. Only then I can examine the state of the program in the context that produced the problem.

So what to do? In the program I replace the signal handler for SIGWARN with my own handler that checks for the typical format of a Perl warning. I do that because I am interested only in warnings from the Perl interpreter. If this format has been detected, the code branches into a special path where I can set a breakpoint. After that the warning message is printed as before the modification.

Update: thanks to perrin the breakpoint setting is not necessary anymore. Also the BEGIN block can be shifted in the .perldb initialization file (see Re^3: Debugging a program), so the original file remains unchanged.

Example:

use strict; use warnings; BEGIN { $SIG{__WARN__} = sub { my $warning = shift; if ( $warning =~ m{\s at \s \S+ \s line \s \d+ \. $}xms ) { $DB::single = 1; # debugger stops here automatically } warn $warning; }; return; } my $undefined_scalar; print "Scalar has this value: $undefined_scalar, \n"; print "end\n";
When I run the program under the debugger, it stops in the signal handler when the commented line is reached. I give the (r) command to return to the calling subroutine and investigate the context.

In reply to Re: Debugging a program by hexcoder
in thread Debugging a program by leonidlm

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-20 03:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found