Learning the perl debugger: Lesson 2

pbug-workshop-attendees

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

Now that we understand how to run, and quit the debugger it’s time to inspect some data.

02. Your Next Steps

This lesson shows you how to examine variables in the debugger.

Examining Variables

Start by running the relevant script in the debugger:

perl -d variables.pl

Step through the script (with n) until you see:

string is Mary had a little lamb.
main::(variables.pl:8): print "stop pressing 'n' or 'ENTER' now\n";
  DB 

You can now examine the variables that have been declared in the script. Try these commands in the debugger:

  • p $string
  • x $string
  • p @things
  • x @things
  • p %hashof
  • x %hashof

You may have noticed that x %hashof isn’t very easy to parse. Once you realise it’s a hash you can examine the reference for a nicer output:

  • x \%hashof

Slightly less useful in this simple example is:

  • x \@things

In your day-to-day debugging you will usually use x for examining the value of variables.

The debugger isn’t strict

The perl debugger doesn’t run in ‘strict mode’ so you can examine variables that have not been defined.

  DB x $my_made_up_thing
0  undef

Lesson Summary

  • x – examine a variable
  • p – print the value of a variable
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