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
main::(deeper.pl:19): my $input_value = $ARGV // 6; DB n main::(deeper.pl:20): my $output_value = level1($input_value); DB
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
Notice how the prompt has changed to indicate that we’re no longer in the
top level of a script (
DB s main::level1(deeper.pl:13): my $v = shift;
s until you enter the
DB s main::level3(deeper.pl:4): return $_ + 1;
From here you can use
n to continue following the program flow until
you return back out of the call to
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
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.
r command until you return back from the call to
Explore the script
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.
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
s– step into a function
r– return from a function
c– continue execution
With the commands you’ve learned so far you will be surprised how much
debugging is possible.
q will enable
you to step through most scripts and modules.