Some of the most used debugger Perl commands are shown, also some techniques to get things easier.


Reference Tutorials Faqs Cpan
Funcions Operators Variables Debugger

Debugger. Notes. Small Command Summary

Packages PadWalker and Term::ReadLine::Perl are quite useful for debugging

cpan Term::ReadLine::Perl ## Enable Backspace and arrow keys  ( Backspace ).
                          ## Avoid characters ^[[A", "^[[C", "^[[B", "^[[D"", "^H"
yum install perl-PadWalker


perl -d
perl -de 0 # Without any program. Useful to check expressions.

Summary of useful commands

a [línea] Actiona print 4Print number 4 before current line execution
b Set a breakbSet a break on current line
b 110On line 110
b computeOn subroutine compute
b 110 /Hello/On line 110 if $_ =~ /Hello/
B Delete breakB *All breaks
B 110On line 110
c Continuec 110Run program until line 110 and stop
c computeRun until subroutine compute and stop
L [a|b|w] ListLSows breaks, actions and watches
v Show sourcevArount actual line
v 100Around line 100
l list source linesl 100-110starting line 100 to 110
l 100+5shows 5 lines from line 100
l computeShow first lines form subroutine compute
. Move debugger pointer.Move debugger pointer to the next line being executed
s Next instructionsExecute next line, descending into subroutines calls
n Next instructionnExecute next line, without descending inside subroutines
r Exit subroutinerContinue program execution until return of current subroutine
CR Carrige Return keyCRrepeat n o s command
p Show expression resultp 5 + 510
p $a$a variable value
p $a > 5print 1 (true) if $a > 5 or nothing
x [level] Show expression value. Recursivex $a$a value
x @myArraymyArray values
x \@myArraymyArray values as reference
x 2 \%Mykeysdescend 2 levels, showing %Mykeys values
V [package [vars]]V DB filename lineShow variables $DB::filename y $DB::line
V DB ~fileShow DB package variables whose names match 'file'
V DB !fileShow DB package variables whose name don't match 'file'
X --
y [level[vars]]--
S Show matching subroutinesS compuShow subroutines whose names matches compu
M Show loaded modulesMModules and releases
m packagem ConfigShow package Config methods
o option=valueoShows all options
o windowSize=15Changes number of lines showed with v command ( default 10 )
o arrayDepth=5print only the first 5 elements of an Array
source filesource commands.txtloads file commands.txt and executes it as debugger commands
{{ action{{ v"v" action is done ( show lines around current ) each prompt
{*{*delete all actions set with {{
RRrestar program in debugger
qqquit debugger

B There is not command to delete breaks on subroutines. Display breaks with 'L' and delete them using the line number.

Display values: "p", "x", "V", "X" y "y". "p" show expressions and is like 'print'. "x" shows an array or hash recursively, V shows variables inside a packet matching an expression. "X" is the same as "V" currentpackage. "y" shows lexical variable in actual environment, ("my" defined variables ) or in superior levels according its parameter, PadWalker package is required for this to work.

Debugger configuration. .perldb

Perl debugger tries to find file ".perldb" at current directory first, following to "HOME" directory, if not succesful, when start running. For security reasons ".perldb" must be owned by the current user or superuser, and only the owner must have write permissions on it. This file allows to preserve debugger configurations e.g. "o windowSize=15".

If a subroutine exists called "afterinit" in ".perldb" it is executed after debugger is initialized. I use this property to make a configuration file for each perl file in a directory, as follows:

How to

Including these lines of code at $HOME/.perldb.

sub afterinit {
sub initFile {
  my ($file)= ($0 =~ /([^\/]+)$/);
  if ($file) {
    $file .= '.pldb';
    if ( -r $file ) {
      do { print "$file Not executed. Bad permissions.\n"; return;} if (stat($file))[2] & 0022;
      print "Loading $file\n"; 
      push @DB::typeahead,"source $file";  
    } else { 
      print "Configuration file $file not found\n";

initFile tries to find a file with the same name as the one being debugged, ($0 variable ) but with .pldb exension. If it success, access permissions are checked, ( only the owner can modify it ) and loads it with the debugger "source command". This way I can keep different debugger configurations for each perl file in a directory without modifying .perldb.

For security reasons, .pldb file can only be modified by the owner e.g. "chmod 0644 mytest.pldb".

Example 1. I wish to debug "". I create and write down these initial instructions in "mytest.pldb". As perl debugger is perl, I use the # for comments, as usual.

{*    # Delete all commands set with {{ 
{{ v  # Show lines around, each prompt.
b 200 # break on line 200
o windowSize=12 # print 12 source lines with v or l commands.

Example 2. To set a break in a package, it must be loaded. Here debugger starts until line 3, and then a break is set in subroutine Display::display that could not be found before the 'require' instrucction is finished.

require Display;
my $localVariable;

c 3
b Display::display