The Perl Debugger
The following seven commands are sufficient for basic debugging:
s: single-step execute the next line, stepping into subroutines.
n: single-step execute the next line, stepping over subroutines.
r: nonstop execute until the return from the current subroutine.
c <line-number>: nonstop execute until a particular line.
l <line-number, range or subroutine>: list source code.
x <expression>: evaluate and pretty-print <expression>.
q: quit debugger.
To try these out, run the test program with the debugger:
perl -d sample.pl
You should see debugger startup information:
Default die handler restored. Loading DB routines from perl5db.pl version 1.07 Editor support available. Enter h or h h for help or man perldebug for more help: main::(sample.pl:6): my $name = "Pengu"; DB<1>
This is the state before the program starts running. The next-to-last line has useful information about the debugging status: you're in the main package, file sample.pl line 6, and it displays the line that is about to be run.
The last line is a prompt with the command number (incrementing as you enter more commands) and angle brackets, where the number of angle brackets signifies nested commands. You don't need to worry about those here.
Type s at the prompt and press Enter to single-step one line into the program:
DB<1> s
main::(sample.pl:8): foreach (1..20) {
DB<1>
To repeat the command, press Enter; repeat this as long as you like to be convinced that the program is stepping through its paces. Every time you pass the print statement, it is echoed to the screen, interspaced with the debugging materials.
Now, try the command to step over subroutines (n), and press Enter a few times. You go through the loop and receive your subroutine results right away, without stepping through each command in the subroutine.
Next, try the command to return from the current subroutine (r). But wait—if you do it now, it will run until the program finishes, because you're “returning” from the main program. First, do a couple repetitions of s to step into the subroutine. Then, with an r, you should see something like:
DB<1> s
main::(sample.pl:8): foreach (1..20) {
DB<1>
main::(sample.pl:9): &shout($name);
DB<1>
main::shout(sample.pl:13): my $name = shift;
DB<1> r
*** Pengu ***
void context return from main::shout
main::(sample.pl:8): foreach (1..20) {
DB<1>
Notice the void context return from main::shout line. If we had asked for a return value in the main loop, we would see it displayed here. In Perl, functions and subroutines can return different values based on the context of the caller (scalar, array or void). A nice feature of the Perl debugger is the r command, which tells you what context was requested by the caller. It can find the bug if you ask your subroutine for a scalar, but you mistakenly have the subroutine return an array.
Next, we have the l command. Try it now:
DB<1> l
8==> foreach (1..20) {
9: &shout($name);
10 }
11
12 sub shout {
13: my $name = shift;
14: print "*** $name ***\n";
15 }
DB<1>
Alone, l lists a page of the source code, starting at the next line to be executed, with a text arrow pointing to the next line. You also can list a range by specifying the line numbers, such as l 200-230. Additionally, you can list a subroutine by naming it: l shout.
The c command continues execution until you hit a particular line number, so you can jump ahead to a particular piece of code that is interesting:
DB<1> c 14 main::shout(sample.pl:14): print "*** $name ***\n"; DB<1>
You can execute any Perl expression, including code that changes the running program, by typing it at the prompt. This can include setting variables in the program by hand.
The x command evaluates and pretty-prints any expression, prepending a numbered index on each line of output, dereferencing anything that can be dereferenced and indenting each new level of dereferencing. As an example, below we set an array, @sample, and then display it:
DB<1> @sample = (1..5) DB<2> x @sample 0 1 1 2 2 3 3 4 4 5 DB<3>
Notice that hashes are displayed with keys and values, each one on a line. You can display hashes properly by preceeding the hash with a \, which turns the hash into a hash reference, which is properly dereferenced. This looks like:
DB<4> %sample = (1 .. 8) DB<5> x \%sample 0 HASH(0x83d53bc) 1 => 2 3 => 4 5 => 6 7 => 8 DB<6>
When you are satisfied with the results, quit the debugging exercise with q.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- A Topic for Discussion - Open Source Feature-Richness?
- Drupal Is a Framework: Why Everyone Needs to Understand This
- Home, My Backup Data Center
- What's the tweeting protocol?
- Readers' Choice Awards
- New Products
- RSS Feeds
- Dart: a New Web Programming Experience
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




10 hours 15 min ago
12 hours 48 min ago
14 hours 5 min ago
14 hours 40 min ago
15 hours 3 min ago
19 hours 51 min ago
20 hours 38 min ago
22 hours 12 min ago
23 hours 48 min ago
1 day 1 hour ago