A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

Jump to

Quick reference

Showing posts with label debugger. Show all posts
Showing posts with label debugger. Show all posts

Try::Tiny debugger walkthrough

You are using the perl debugger. You enter Try::Tiny. Here is the solution:

Start on the final }; of the catch

s
n x 3,
s
n x 10,
s
n x 4

You will now be at:
if ( $wantarray ) {

You need to go 's' through one of the $try->()

You should now be inside the try {} block

How to defeat Sub::Quote and Sub::Defer

Do you use the Perl debugger?

Are you frustrated when you hit a maze of Sub::Quote twisty passages, all alike?

Then this guide is for you!

Walkthrough solution for Sub::Quote:

  • n (north)
  • n (north)
  • s (south)
  • # Boom! You should now be on the other side of the maze.
Walkthrough solution for Sub::Defer:
  • to do
(source: Text adventures of the eighties and nineties, and Perl coding adventures of the naughties)

Get command history in the perl debugger

Problem:

When you press "up", you don't get the previous command.

Solution:

Install module Term::ReadLine::Gnu

"up" will now work.

(source)

Perl debugger tutorial

If you've never used the Perl debugger before, it is pretty easy when you know how:

1) Add this special breakpoint statement to your script, where you want it to stop so you can look around:
$DB::single = 1;

2) Run the script with -d like this:
perl -d myscript.pl

The script will stop at the first line of code. Press 'c' to continue on to the first breakpoint.

Basic commands:
n = next line (step over any subs)
s = next line (step into any subs)
c = continue

one more command:
v = view lines

There's a trick to make it do 'v' at every step, type this once:
{{v

Instructions:
Step through the script using either 's' or 'n' and make sure it's doing what you think it's doing.

You might get into some gnarly library code at some point. In that case you'll have to either:
a) endure it and keep stepping through until you reach your code on the other side
or
b) set a breakpoint a bit later on, then restart the debugger and keep pressing 'c' until you reach the right place

The difference between 's' and 'n' is, when executing any subroutine call:
- 'n' steps over the subroutine (runs it) and carries on
- 's' steps into the subroutine and you can step through the code inside it

Finally:
You can type any perl statement at the debugger prompt to see what values the variables and things have, for example:
use Data::Dumper;
print Dumper($foo);

Other commands:
q = quit
h = help

See also perldoc perldebug and perldoc perldebtut.

--
Thanks to Nour for being a guinea pig.