Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

First, if you've never used the debugger before, getting started is so easy; just prepend "perl -Sd" to the command line of the script you wish to debug. The debugger will remind you to type "h h" for quick help and will show you the next line that is about to be executed. Type "n" (for "next") to execute that line. Type "s" (for "step") to start executing that line but stop if a subroutine is called so you can debug it. Type "c mysub" (for "continue") to run until "sub mysub" is called. Type "x" followed by any Perl expression to see what values result. Type any Perl code to have it executed.

perl -Sd scriptname -scriptoptions scriptarguments DB<1> h h DB<2> |h
The "-S" tells Perl to find your script where ever it is in your $ENV{PATH}. The "-d" tells Perl you want the debugger. The "h h" gives you help on the "help" command, which is a quick one-screen run-down of all of the debugger commands. "|h" pipes the output of the full "help" command through your default "more"-like program so you can read it all.

I use the debugger frequently. Although the turn-around time between adding print statements and rerunning your script is much lower in Perl than in a more traditional compiled language, it is still much, much slower than adding the print statements interactively only where and when you want them via the debugger. Especially since you can type x $self and see complex nested data. Even if you've gone out of your way to make "print" debugging easy, you can't add as useful of a print statement to your code with fewer keystrokes.

You can test possible fixes to your bugs right there in the debugger. "Oops, off by one," then just add or subtract one right there and see if the next result becomes correct. Say the debugger has convinced you that you have a complex bug that will take some work to correct. Just replace the incorrect result with the correct one and you can continue verifying or debugging the rest of your code in the mean time.

If you've never used the debugger, then I'll bet you've never run into a bug that manifests itself late in a long-running script. Debugging those with print statements is very frustrating ("Oops, didn't print enough. Now I've got to wait another 20 minutes to see if I located where the bug really is this time", "Oops, shouldn't have put that print statement there. It will take me an hour to cull through all of that output and it is making the run take three times as long").

There are three things that I notice the debugger is currently not suited for: playing with "-w", "use strict", and regular expressions. The first is just a design decision that means that "-w" is not in effect for code that you type at the debugger prompt. This is easy to patch but has not been done yet. The second is similar but would probably need to be fixed as a debugger "option".

The last has to do with the debugger's valiant but still-insufficient attempts to save and restore the complex regex context for you. The debugger itself uses regexen to figure out what command you just entered. It tries very hard to save and then restore the state of all of the regex-related variables associated with the code you are debugging so that its own use of regexen doesn't change the behavior of your program. But it doesn't always succeed. This is especially true if you are typing regexen at the debugger prompt. If you aren't getting what you expected, then ask for your results with the same line of input that performed the regex (which will still fail for the new and highly magical @- and @+).

I also use the debugger interactively via "perl -de 0" even more often. It is useful for making quick calculations, playing with ideas that will turn into scripts, exploring the language and modules, or just getting one-off work done. My Win32API::File and related modules are designed for easy use from the debugger. I've used them to do things like interactively compare and correct boot sectors. If you are on Win32, you should try this:

perl -de 0 DB<1> use Win32API::File ":ALL" DB<2> |m Win32API::File [...] createFile [...] DB<3> createFile() Win32API::File::createFile() usage: $hObject= createFile( $sPath, [$svAccess_qrw_ktn_ce,[$svShare_rwd,]] [{Option=>$Value} +] ) options: Access Create Share Attributes Flags Security Model Called at (eval 7)[F:/Apps/Perl/lib/] line 2 DB<4> createFile("","?") Win32API::File::createFile: $svAccess can use the following: One or more of the following: q -- Query access (same as 0) r -- Read access (GENERIC_READ) w -- Write access (GENERIC_WRITE) At most one of the following: k -- Keep if exists t -- Truncate if exists n -- New file only (fail if file already exists) At most one of the following: c -- Create if doesn't exist e -- Existing file only (fail if doesn't exist) '' is the same as 'q k e' 'r' is the same as 'r k e' 'w' is the same as 'w t c' 'rw' is the same as 'rw k c' 'rt' or 'rn' implies 'c'. Or $svAccess can be numeric. Called from at (eval 13)[F:/Apps/Perl/lib/] line 2 DB<5>
See, you don't even need to remember how to use the module.

        - tye (but my friends call me "Tye")

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

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (6)
    As of 2018-05-25 19:02 GMT
    Find Nodes?
      Voting Booth?