How to use the Perl debugger perl5db.pl

The Perl Debugger, perl5db.pl is a command-line debugging tool that comes with the Perl interpreter by default. Since perl5db.pl is not a GUI-based program, it can be tricky, especially for Perl beginners. It is fairly similar to gdb. Let’s have a look at some basic functionalities of perl5db.pl.

The Perl debugger can be invoked using the following command:

Perl -d <your_perl_script>

Stepping through the code

n lets you step to the next line in the code. It is used when you want to step through the code line by line.

However, if you want to step into a sub-routine or function, use the s command.

Break-points

perl5db.pl lets you set breakpoints using the b command. b sets a permanent breakpoint, but if you want to set a temporary breakpoint, use the c command.

The c command runs through the program until the breakpoint is reached.

DB<1> c <subroutine>
DB<1> b <subroutine>

Looking at specific lines

The l command is used to view specific lines:

#Code Snippet
print "This is the first line\n"
print "This is the second line\n"
#Debugger output
main::(helloWorld.pl:1):	print "This is the 1st line\n";
DB<1> l 2
2:	print "This is the second line\n";

Printing the value of a variable

The p command prints the value of the variable to the output:

p $<var_name>

You can visit the Perl documentation to view more features of the Perl debugger.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved