The Perl Debugger
There are two ways to execute the code. We know that the current line is 3 and is an if statement. The first method, s, is to step through the code, statement by statement. The other method is n, for next, which similarly steps through the code; however, in the case where the current statement is a subroutine call (as opposed to a built-in function or some sort of variable assignment), n will treat the subroutine as though it were a built-in function and will step over the subroutine, as if it is an atomic command. In contrast, s will enter the subroutine and step through every line of the subroutine. It will do the same for any subroutines encountered within the first one. This can be annoying when we know that the subroutine is working correctly—hence the n command. For this simple example, where we have no subroutines, n has the same effect as s. After entering s or n, we can simply press enter, and the debugger will reissue the last s or the last n command. This is useful to get through lines of code quickly. Pressing s displays the following:
main::(./p1.pl:7): while ($i < 10) {
Notice that we've skipped from line 3 to line 7. Enter l 3+4. This shows us four lines from line 3. We skipped to line 7 because the conditional in line 3, if(0), is false. So the then part of the conditional is ignored, and the else portion is executed.
Notice that there is a variable in the code, $i. We know that the body of the while loop will be executed until $i is greater than or equal to 10. (Enter l 7+10 to see the body of the while loop.)
So what value does $i have now? Type p $i. The print command is p, and without an argument; it will print the contents of the magic Perl variable $_. Any valid Perl expression is a valid argument to p. Because anything that the debugger doesn't recognize as a debugger command is evaluated as Perl code, you could also type print instead of p. Don't worry about having redirected standard output to something other than your screen. The debugger will take care to ensure that you'll see some output. But, typing p is quicker than typing print, and as any good programmer knows, laziness is one of the “programmer's virtues”, the other two being hubris and impatience (Larry Wall, see Resources).
Typing p $i results in nothing. No, we didn't do anything wrong. $i hasn't been set to anything, so it gets the default value of nothing. Type s (or just press enter). Try p $i again. It should print the number 1. Press enter again and type p $i again. Now, we could continue this, but we know that we will keep spinning in this while loop until the conditional returns false, which won't happen until $i is no longer less than 10. And, as I said before, impatience is another programmer's virtue, so we'll rush things along a bit. Enter $i = 8, then press enter again. Do it one more time, and we've broken free of the loop.
The last conditional checks that $i is at least equal to 9. Because it now is, the then portion of the if statement will not get executed. Note that we could have set $i back to 2 before we execute the final if statement. The result would have been an execution that under normal conditions (i.e., without using the debugger) could never have occurred (assuming the computer is working properly, and no bits in memory get fiddled).
As any good first program should, our first debug program prints Hello, World! to the screen. Notice that even under the debugger, this happens as it should. Pressing enter one more time terminates the program.
The code in Listing 2 is a more complex piece of code with a bug in it. It should print out every regular file in the current directory and all subdirectories, recursively. Right now, it only prints the files in the current directory and doesn't seem to delve into further subdirectories.
Execute this program in a directory with a few subdirectories and place files and further subdirectories in these subdirectories to create a small but diverse hierarchy.
The output of this code (once the bug gets fixed) from the directories I ran it in, looked like this:
./file1 ./dir1.0/file1 ./dir1.0/file2 ./dir1.0/file3 ./dir1.0/dir1.1/file1 ./dir1.0/dir1.1/file2 ./dir1.0/dir1.1/file3 ./dir2.0/file1 ./dir2.0/file2 ./dir2.0/file3 ./dir2.0/dir2.1/file1 ./dir2.0/dir2.1/file2 ./dir3.0/file1
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
| Designing Electronics with Linux | May 22, 2013 |
| 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 |
- Linux Systems Administrator
- New Products
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
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!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
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?




5 hours 43 min ago
10 hours 15 min ago
10 hours 16 min ago
12 hours 16 min ago
21 hours 1 min ago
21 hours 35 min ago
22 hours 34 min ago
23 hours 24 min ago
1 day 3 hours ago
1 day 7 hours ago