Raleigh, NC

 

July 2005 Meeting

Date: Thursday, July 21, 2005 at 7:15 PM
Presenter: Mike South
Notes:
0) Perl has a debugger

1) Invoking the debugger
    a) perl -d
        even if you normally run the script as "scriptname arg1 arg2" etc, 
        you can run it with "perl -d scriptname arg1 arg2" and it will use
        the args, etc the same way

    c) perl -de1
        
        perl -e 'print join("\n",@INC)' 

            runs the line in single quotes as a one-line script (or more
            lines if you put semicolons in there, etc).

        perl -de 'print join("\n",@INC)' 

            runs that one[or more]-line script in the debugger
       
        as a convenience, you can make the one line of the one-line script be '1',
        and you can then do whatever you want in the debugger.

        This is useful for trying out a line of code like a regex that you want to
        test against a couple of kinds of input:


            [msouth@localhost ~]$ perl -de1

            Loading DB routines from perl5db.pl version 1.27
            Editor support available.

            Enter h or `h h' for help, or `man perldebug' for more help.

            main::(-e:1):   1
            DB<1> $foo = 'I have 2 numbers in this 1'

            DB<2> @matches = $foo=~/\d+/;

            DB<3> x @matches
            0  1
            DB<4> @matches = $foo=~/\d+/g;

            DB<5> x @matches
            0  2
            1  1


    b) re-run after script completes (or any other time) 
        with 'R', preserving breakpoints

        When a script you are debugging exits (either normally or
        through an explicit die() or fatal exception), you can quit the
        debugger with 'q'.  But wait!  You might not want to 'q'uit yet.
        All your breakpoints, actions, and watchpoints are still there.
        You can hit 'R' to re-run (with the same args and everything)
        and all your breakpoints, watch expressions, and actions intact.

2) Basic listing, stepping, and display of data
    a) list with 'l', '-', '.'

        list shows you a 'window' of lines around 
        the line you are on.  By default, the next 
        ten lines (change that with "o windowSize=20").

    b) back to where you once belonged, with '.'

    c) step over with 'n'

        'n' runs the current line without stepping into
        any subroutines the current line calls

    d) step in with 's'

        's' steps into subroutines that the current line
        calls
        
    e) return with 'r'

        returns from the current subroutine you are
        stepping through and shows you the return
        value.

    f) examine data with 'x' (use ref trick)

        x $foo
        x \%hash (easier to see what is going on with 
                    \%hash than %hash, which will show
                    it was a linear array)

3) Breakpoints
    a) temporary breakpoints with 'c'
        
        c 56

        'continue until you hit line 56'

        c Foo::Bar::baz

        'continue until you hit sub Foo::Bar::baz'

    b) persistent breakpoints with 'b'

        b 56

        'break every time you hit line 56'

        b Foo::Bar::baz

        'continue until you hit sub Foo::Bar::baz'

4) Long distance breakpoints

    a) fully qualified sub names
        
        set a break point on any subroutine
        in any package that has been loaded.

        b NM::TmplSys::Page::build

        
    b) switching files with 'f'
        
        f /home/msouth/shared/codebase/tmplsys/lib/perl/NM/TmplSys/Template.pm

        switches to the file (if it has been loaded into memory)

       i) breaking on file load

            b load /home/msouth/shared/codebase/tmplsys/lib/perl/NM/TmplSys/Template.pm

            will break when that file is loaded

5) Watch expressions

    w expr

    triggers a break every time expr changes

6) Debugging code while it's running under mod_perl

    a) Pure handlers are easy
    b) Registry scripts are hard