While reading through the Perl documentation, I came across a good overview of the Perl debugger. This is a nifty utility would have been helpful while I was writing ldap-stats.pl. If you want to debug a specific Perl script, you can add the “-d” option to the perl interpreter:
$ head -1 ldap-stats.pl
#!/usr/bin/perl -w -d
Once this option is added, program execution will transfer to the debugger, allowing you to set breakpoints, and watch the execution of your program:
$ ldap-stats.pl
Loading DB routines from perl5db.pl version 1.19
Editor support available.
Enter h or `h h' for help, or `man perldebug' for more help.
main::(./ldap-stats.pl-2.2.pl:195):
DB<1>
If you type “h” in the debugger’s help screen, you will get the following help screen:
DB<1> h
List/search source lines: Control script execution:
l [ln|sub] List source code T Stack trace
- or . List previous/current line s [expr] Single step [in expr]
v [line] View around line n [expr] Next, steps over subs
f filename View source in file Repeat last n or s
/pattern/ ?patt? Search forw/backw r Return from subroutine
M Show module versions c [ln|sub] Continue until position
Debugger controls: L List break/watch/actions
o [...] Set debugger options t [expr] Toggle trace [trace expr]
< [<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
! [N|pat] Redo a previous command B ln|* Delete a/all breakpoints
H [-num] Display last num commands a [ln] cmd Do cmd before line
= [a val] Define/list an alias A ln|* Delete a/all actions
h [db_cmd] Get help on command w expr Add a watch expression
h h Complete help page W expr|* Delete a/all watch exprs
|[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess
q or ^D Quit R Attempt a restart
Data Examination: expr Execute perl code, also see: s,n,t expr
x|m expr Evals expr in list context, dumps the result or lists methods.
p expr Print expression (uses script's current package).
S [[!]pat] List subroutine names [not] matching pattern
V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
X [Vars] Same as "V current_package [Vars]".
y [n [Vars]] List lexicals in higher scope . Vars same as V.
For more help, type h cmd_letter, or run man perldebug for all docs.
DB<1>
For navigating through code, the “T” (stack trace), “s” (single step), “n” (next step), “r” (return from subroutine), “c” (continue until next position) and “b” (set breakpoint) options will prove super useful. I think the “M” (print modules and versions) is my favorite option:
DB<1> M
'AutoLoader.pm' => '5.59 from /usr/local/lib/perl5/5.8.0/AutoLoader.pm'
'Carp.pm' => '1.01 from /usr/local/lib/perl5/5.8.0/Carp.pm'
'Carp/Heavy.pm' => '/usr/local/lib/perl5/5.8.0/Carp/Heavy.pm'
'Config.pm' => '/usr/local/lib/perl5/5.8.0/sun4-solaris/Config.pm'
'DynaLoader.pm' => '1.04 from /usr/local/lib/perl5/5.8.0/sun4-solaris/DynaLoader.pm'
'Exporter.pm' => '5.566 from /usr/local/lib/perl5/5.8.0/Exporter.pm'
'Getopt/Std.pm' => '1.03 from /usr/local/lib/perl5/5.8.0/Getopt/Std.pm'
'SelfLoader.pm' => '1.0903 from /usr/local/lib/perl5/5.8.0/SelfLoader.pm'
'Term/Cap.pm' => '1.07 from /usr/local/lib/perl5/5.8.0/Term/Cap.pm'
'Term/ReadKey.pm' => '2.21 from /usr/local/lib/perl5/site_perl/5.8.0/sun4-solaris/Term/ReadKey.pm'
'Term/ReadLine.pm' => '1.00 from /usr/local/lib/perl5/5.8.0/Term/ReadLine.pm'
'Term/ReadLine/Perl.pm' => '0.99 from /usr/local/lib/perl5/site_perl/5.8.0/Term/ReadLine/Perl.pm'
'Term/ReadLine/readline.pm' => '1.0203 from /usr/local/lib/perl5/site_perl/5.8.0/Term/ReadLine/readline.pm'
'perl5db.pl' => '1.19 from /usr/local/lib/perl5/5.8.0/perl5db.pl'
'strict.pm' => '1.02 from /usr/local/lib/perl5/5.8.0/strict.pm'
'vars.pm' => '1.01 from /usr/local/lib/perl5/5.8.0/vars.pm'
'warnings.pm' => '1.00 from /usr/local/lib/perl5/5.8.0/warnings.pm'
'warnings/register.pm' => '1.00 from /usr/local/lib/perl5/5.8.0/warnings/register.pm'
This option will print the modules required to run the application, and the current version of each module. I will cover program flow under the Perl debugger in a subsequent BLOG entry.