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.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
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
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| 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 |
- RSS Feeds
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Using Salt Stack and Vagrant for Drupal Development
- Dynamic DNS—an Object Lesson in Problem Solving
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Download the Free Red Hat White Paper "Using an Open Source Framework to Catch the Bad Guy"
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout 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 Pi Cobbler Breakout 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
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




20 min 38 sec ago
2 hours 11 min ago
7 hours 25 min ago
10 hours 36 min ago
12 hours 51 min ago
13 hours 20 min ago
14 hours 18 min ago
15 hours 47 min ago
16 hours 55 min ago
17 hours 42 min ago