Perl Programming Page 6 - Using The Perl Debugger |
Now that you've seen what the debugger can do, let's take it out for a quick test drive. Consider the following simple Perl program, which contains a deliberate error:
This is pretty simple - it accepts a number as input and creates a multiplication table for it. If, for example, you invoked it with the number 4 as argument, you'd expect a multiplication table like this:
Instead, what you get is this:
If you're an experienced Perl programmer, you already know the problem. Don't say a word; instead, just follow along as I run this through the debugger.
Loading DB routines from perl5db.pl version 1.19 Enter h or `h h' for help, or `man perldebug' for more help. main::(multiply.pl:11): @values = (1..10); Now, since I'm having a problem with the product of the two numbers, and that product is being calculated by the multiply() subroutine, it makes sense to set a breakpoint at that subroutine with the "b" command.
I can now step through the script with the "r" command, which will display the return value from the subroutine every time it is invoked.
Hmmm...a return value of zero at every stage is obviously an indication that something is going seriously wrong inside the subroutine. This is probably the reason for the long string of zeroes in the output of my script. Now, if only we could look inside to see what was really going on...
Curiouser and curiouser. As the "x" command clearly shows, the multiply() subroutine is obviously not using correct parameters when performing the Now, there are two possible reasons for this: either the subroutine is being passed incorrect values correctly from the main script, or the subroutine is not reading them properly. Luckily, the first theory is easy enough to test - all I need to do is run the "x" command on the @_ array:
Perfect. So the main script is obviously passing the input arguments correctly to the multiply() subroutine, as evidenced by the fact that the @_ array contains the correct values. The problem is therefore related to the extraction of these values from the @_ array into regular scalars - specifically, with line 6 of the script:
And now the error should be fairly obvious. As any Perl coder knows, when retrieving subroutine arguments from the @_ array into scalars, the list of scalars should be enclosed in parentheses. Corrected, the statement above would read:
Now, when I re-run the script and step through it, the "x" command shows that the subroutine's $a and $b scalars are being populated with the correct values.
And when I run the script, the output now makes sense:
Experienced Perl programmers will, of course, point out that I could have saved myself all this trouble either by placing the
construct at the top of my script, or by adding the "-w" switch when running the script from the command line.
Both these methods would have brought the error to my notice without requiring me to use the debugger. However, in some cases, especially cases involving logical errors that a syntax checker will not catch, the debugger can be an invaluable tool in diagnosing problems with your code, since it allows you to step through each line of code and inspect (or change) the variables being used at every stage to catch potentially-serious bugs. And that's about all for the moment. As you saw over the last few pages, the Perl debugger is a powerful utility, one that can substantially reduce your stress levels when dealing with recalcitrant Perl code. Go ahead, play with it a little...and, until next time, stay healthy! Note: Examples in this article have been tested on Linux/i586 with Perl 5.8.0. Examples are illustrative only, and are not meant for a production environment. Melonfire provides no warranties or support for the source code described in this article. YMMV!
blog comments powered by Disqus |
|
|
|
|
|
|
|