This post follows on from lesson 3 in this series of posts.
You should be comfortable navigating through code and inspecting interesting variables as you go.
Next you’ll learn how to view the files you are debugging without leaving the debugger.
04. Looking Around
This lesson shows you how to view your current location, the code around your
current location and code in other locations from the debugger.
Run the script in the debugger
perl -d view.pl
At the prompt, try the
Notice that however many times you use
. the output is the same; your
current line doesn’t change if you’re only viewing source.
If you use
v multiple times the area of code being viewed moves through the
current file, showing the last line of the previous
. resets the area shown by the next
Looking at specific places
You can give
v a line number as an argument.
v 1 to look at the top of the current file.
You can also look at a single line.
l 1 to see what the first line in the file is.
You can look at blank lines too; try
It’s okay, you can’t fall off!
Try viewing the first line of the file only (
l 1) then use
times until you reach the end of the file.
v a couple of times more at the end of the file and confirm that no matter
how hard you try you can’t view yourself into oblivion.
Then view your current location again by using
. followed by
What’s that sub?
You can list sub routines with
l [sub]. Examine the
If the subroutine is long you will not see all of it in one go.
Quit the debugger. Run:
perl -d long.pl
View the long subroutine:
v to see the whole subroutine a chunk at a time.
If you can only view your file in the debugger, this makes it possible to
explore, but it’s not very friendly; it’s almost always useful to have the actual
source files open in a separate editor or terminal window.
While we’re still looking at
long.pl, it’s a great opportunity to learn how
to look at other source files. It’s pretty easy really, but
f does have an
Try to view
You’ll get an error. No matter what you try to do, you can only view a file
that’s already been loaded. Instead, we can load a module, and view that file:
use Thing f Thing.pm v
Because we loaded our initial script, we can examine the file:
f long.pl v
Examining remote subs
We remember seeing
one() in the
Try viewing the function:
Oops. You need to give the debugger the fully qualified name for methods that
aren’t in your current file:
Notice that examining a subroutine in another file switches you to that file.
l [line|sub]– list source code
v [line]– view around line
.– list current line
f [file]– view source in a file