Here are some tools I use:

I've noticed that a lot of my debugging time is spent debugging the wrong code, i.e the error is somewhere else than I think. This is because you narrow down to quickly on a perceived problem, and because errors get layered on top of each other, so that the cause of the error is actually some minutes, or even days old. One remedy to this is to use

perl -c your_script.pl
every minute or so, so that you catch little syntax errors when they are done. If you're in an error-prone mode otherwise little errors will be layered on top of each other.

A graphical debugger like ptkdb is really good for those (like me:-) who have problem keeping structures in (the brain's) memory. Graphical debugger means see-and-act instead of remember-and-type.

While programming, write little test scripts to test how certain things in perl work, before you rely on them in your scripts. Save the test scripts with a comment on what happened.

Use print statements to check what is actually happening in your script. Use Data::Dumper (in the core distribution) to print out complex structures. Here is a module I use. It is a hack but it seems to do what I want:

package jmdebug; # Debugging module # # # PURPOSE: to provide debugging information concerning methods # # CALLING FORMAT: # jmdebug::output("Description text",$scalarvariable_or_complex_refere +nce); # Put the above line with modification in e.g. any metod calls you wan +t to keep tabs on # In order to include several variables, do something like this: # jmdebug::output("Description text",\[$var1,$var2,\@array1]); # # BEHAVIOUR:checks to see if global var $debug is true, tries to gathe +r information in a subroutine context # which is done with the caller(1) syntax. If that does not work does +a plain caller(0) function call # Tries to pretty-print results use Data::Dumper; # used to display data structures sub output { if ($main::debug){ my ($pack, $filename, $line, $sub, $arg, $context, $dummy, $subnam +e); ($pack, $filename, $line, $sub, $arg, $context) = caller(1) or ($ +pack, $filename, $line, $dummy, $arg, $context) = caller(0); $text = shift @_; my $datastructure = shift @_; $datastructure = Dumper($datastructure); $datastructure =~ s§^\$VAR1 \= §§; $subname = $sub; $subname =~ s§^.*\:\:§§; my ($pre, $post); if ($main::debug_html) { $pre = "<PRE>"; $post = "</PRE>"; } print (qq§ $pre _____ / \\_________________________________________________ | | $subname : | ** $text ** | package: $pack | line: $line | in subroutine: $sub variable contains: $datastructure | |__________________________________________________________ $post§);} } 1;

Finally, write test scripts to give your subs or methods a run-through with different parameters and scenarios.

/jeorgen


In reply to RE: debugging by jeorgen
in thread debugging by toadi

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.