Learning the perl debugger: Lesson 3


This post follows on from lesson 2 in this series of posts.

If you’ve followed the lessons so far you’ll have some basic knowledge around starting the debugger and examining variables.

It’s time to find out how to descend into the depths of some code, stepping into functions.

03. Digging Deeper

This lesson shows you how to step into functions.

Racing through the script

Run the script in the debugger

perl -d deeper.pl

Step through the script (with n) until you see the ‘quit debugger message’.

This is the quickest way to progress through a script. It’s also, often, the
least useful and least informative.

Stopping to smell the flowers

Restart the script in the debugger with R. Step over the assignment to
$input_value and stop when you see that the debugger is about to make the
call to the level1() function:

main::(deeper.pl:19):   my $input_value = $ARGV[0] // 6;
  DB n
main::(deeper.pl:20):   my $output_value = level1($input_value);

This time, instead of running this statement and continuing at this level, we’ll
‘step into’ the function on the current line.

Enter the command s.

Notice how the prompt has changed to indicate that we’re no longer in the
top level of a script (main::):

  DB s
main::level1(deeper.pl:13):     my $v = shift;

Keep using s until you enter the level3() function:

  DB s
main::level3(deeper.pl:4):      return $_[0] + 1;

From here you can use s or n to continue following the program flow until
you return back out of the call to level1():

  DB n
main::(deeper.pl:22):   print "we got an output of $output_value, using $input_value\n";

Returning to surface level

If you step into a function that is of no interest to you, you can quickly get
back to the calling line using r.

Restart the script debugging (with R) and step down into the functions until
you enter the level3() function again.

Enter the command r. Notice that returning from a function outputs the
return value from the function you are in.

Use the r command until you return back from the call to level1().

Explore the script

Use n, s, and r to explore the script. Vary your use of the three
commands and get a feel for how they are used and how they behave.

Don’t forget to use x along the way to examine the values in the variables.

Bored now!

If you get bored of stepping through a script you can use c to continue the
script. Try it when you are in one of the deeper functions.

In our simple example, c will cause the script to run to completion. In more
advanced scenarios, it might only continue script execution until the next

Lesson Summary

  • s – step into a function
  • r – return from a function
  • c – continue execution

Well done!

With the commands you’ve learned so far you will be surprised how much
debugging is possible. n, s, r, x, p, c, R and q will enable
you to step through most scripts and modules.

Print Friendly
This entry was posted in Perl, Tutorial and tagged , , , , , , by Chisel. Bookmark the permalink.

About Chisel

I've been interested in technology since my childhood (my Acorn Electron era). Commercially I've worked with C and VBA (Access) [but we don't talk about that]. In 2000 I secured my first role as a Perl Programmer and that's been my primary language since then. I dabble in bash, puppet, and a few other skills that help me dip my toe in the DevOps water, or provide a conduit beween the dev and ops worlds. I joined Net-A-Porter in November 2006 and have been happily evolving with the business since then.

Leave a Reply