≡ Menu

Perl Debugger Tutorial: 10 Easy Steps to Debug Perl Program

Earlier we discussed the basics of how to write and execute a perl program using Perl Hello World Example.

In this article, Let us review how to debug a perl program / script using Perl debugger, which is similar to the gdb tool for debugging C code.

To debug a perl program, invoke the perl debugger using “perl -d” as shown below.

# perl -d  ./perl_debugger.pl

To understand the perl debugger commands in detail, let us create the following sample perl program (perl_debugger.pl).

$ cat perl_debugger.pl
#!/usr/bin/perl -w

# Script to list out the filenames (in the pwd) that contains specific pattern.

#Enabling slurp mode
$/=undef;

# Function : get_pattern
# Description : to get the pattern to be matched in files.
sub get_pattern
{
my $pattern;
print "Enter search string: ";
chomp ($pattern = <> );
return $pattern;
}

# Function : find_files
# Description : to get list of filenames that contains the input pattern.
sub find_files
{
my $pattern = shift;
my (@files,@list,$file);

# using glob, obtaining the filenames,
@files = <./*>;

# taking out the filenames that contains pattern.
@list = grep {
$file = $_;
open $FH,"$file";
@lines = <$FH>;
$count = grep { /$pattern/ } @lines;
$file if($count);
} @files;
return @list;
}
# to obtain the pattern from STDIN
$pattern = get_pattern();

# to find-out the list of filenames which has the input pattern.
@list = find_files($pattern);

print join "\n",@list;

1. Enter Perl Debugger

# perl -d ./perl_debugger.pl

it prompts,
DB<1>

2. View specific lines or subroutine statements using (l)

DB<1> l 10
10: my $pattern;

DB<2> l get_pattern
11 {
12: my $pattern;
13: print “Enter search string: “;
14: chomp ($pattern = );
15: return $pattern;
16 }

3. Set the breakpoint on get_pattern function using (b)

DB<3> b find_files

4. Set the breakpoint on specific line using (b)

DB<4> b 44

5. View the breakpoints using (L)

DB<5> L
./perl_debugger.pl:
22: my $pattern = shift;
break if (1)
44: print join “\n”,@list;
break if (1)

6. step by step execution using (s and n)

DB<5> s
main::(./perl_debugger.pl:39): $pattern = get_pattern();

DB<5> s
main::get_pattern(./perl_debugger.pl:12):
12: my $pattern;

Option s and n does step by step execution of each statements. Option s steps into the subroutine. Option n executes the subroutine in a single step (stepping over it).

The s option does stepping into the subroutine but while n option which would execute the subroutine(stepping over it).

7. Continue till next breakpoint (or line number, or subroutine) using (c)

DB<5> c
Enter search string: perl
main::find_files(./perl_debugger.pl:22):
22: my $pattern = shift;

8. Continue down to the specific line number using (c)

DB<5> c 36
main::find_files(./perl_debugger.pl:36):
36: return @list;

9. Print the value in the specific variable using (p)

DB<6> p $pattern
perl

DB<7> c
main::(./perl_debugger.pl:44): print join “\n”,@list;
DB<7> c
./perl_debugger.pl
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.

After the last continue operation, the output gets printed on the stdout as “./perl_debugger.pl” since it matches the pattern “perl”.

10. Get debug commands from the file (source)

Perl debugger can get the debug command from the file and execute it. For example, create the file called “debug_cmds” with the perl debug commands as,

c
p $pattern
q

Note that R is used to restart the operation(no need quit and start debugger again).
DB<7> R
DB<7> source debug_cmds
>> c
Enter search string: perl
./perl_debugger.pl
Debugged program terminated. Use q to quit or R to restart,
use o inhibit_exit to avoid stopping after program termination,
h q, h R or h o to get additional info.
>> p $pattern
perl
>> q

Note: If you are relatively new to perl, refer to our previous article: 20 perl programming tips for beginners.

Summary of perl debugger commands

Following options can be used once you enter the perl debugger.

  • h or h h – for help page
  • c – to continue down from current execution till the breakpoint otherwise till the subroutine name or line number,
  • p – to show the values of variables,
  • b – to place the breakpoints,
  • L – to see the breakpoints set,
  • d – to delete the breakpoints,
  • s – to step into the next line execution.
  • n – to step over the next line execution, so if next line is subroutine call, it would execute subroutine but not descend into it for inspection.
  • source file – to take the debug commands from the file.
  • l subname – to see the execution statements available in a subroutine.
  • q – to quit from the debugger mode.
Add your comment

If you enjoyed this article, you might also like..

  1. 50 Linux Sysadmin Tutorials
  2. 50 Most Frequently Used Linux Commands (With Examples)
  3. Top 25 Best Linux Performance Monitoring and Debugging Tools
  4. Mommy, I found it! – 15 Practical Linux Find Command Examples
  5. Linux 101 Hacks 2nd Edition eBook Linux 101 Hacks Book

Bash 101 Hacks Book Sed and Awk 101 Hacks Book Nagios Core 3 Book Vim 101 Hacks Book

Comments on this entry are closed.

  • Fredo May 19, 2010, 6:18 pm

    What do you mean with “surf mode” (line 6/7). What you are doing here: You set the OUTPUT_RECORD_SEPARATOR ( $\ ) to undef, which it is by default.
    I never heard of “surf mode”, but if you intended to enable “slurp mode” you need to set the INPUT_RECORD_SEPARATOR ( $/ ) to undef. See perlvar when in doubt (here http://perldoc.perl.org/perlvar.html ). But even this would be totaly useless for this script.
    Furthermore the script is broken, because you forgot in line 15.

  • Fredo May 19, 2010, 6:23 pm

    Ah, i see!
    I couldn’t write less-then STDIN greater-then correct myself in the above example. I gets eaten by the comment function.

  • Dhanaprabhu Arumugam. May 20, 2010, 4:28 am

    Hi Ramesh,

    In the above example script perl_debugger.pl, the line which is written for getting the pattern from the user is not completed. The line might be “chomp ( $pattern = less than STDIN Greater than );” instead “chomp ($pattern = );”. If I’m wrong, Please correct me.

  • Ramesh Natarajan May 20, 2010, 3:05 pm

    @Fredo, @Dhanaprabhu,

    Thanks for pointing out the issues. I’ve corrected it now.

  • pseudocoder May 28, 2010, 11:07 pm

    Hope you will also find following two command useful (Either you did not mention them, or I’ve overseen them);
    They’ll prevent one from running into the interactive mode:

    PERLDB_OPTS=’NonStop AutoTrace’ perl -d perlscript.pl
    perl -d:Trace perlscript.pl

  • Tanuj November 8, 2012, 10:11 pm

    Thank you.
    Very easily explained.

  • Rick Carrick December 3, 2012, 3:44 pm

    I use the uppercase X and lower case for examining variables. With the UPPERCASE X You can eliminate the $ or @ in front of the variable, and examine all of the fields for the variable. Lower case “x” works WITH those characters and is excellent for copy/paste of the variable while you are troubleshooting.

  • Chandan May 16, 2013, 12:28 am

    Very nice tutorial for begginers of perl……It helps me a lot….
    Thank you very much……

  • Nidhi September 5, 2013, 6:54 am

    Thanks,
    It helps !

  • Moran November 5, 2013, 5:57 am

    Great article! 🙂
    Thanks!

  • Juan Spindola January 6, 2017, 12:06 pm

    Great article about the Perl debugger, and very useful also, to avoid read a lot of information in other manuals and to have the necessary information quickly.