PERLDEBTUT
NAME DESCRIPTION use strict Looking at data and -w and w help Stepping through code Placeholder for a, w, t, T REGULAR EXPRESSIONS OUTPUT TIPS CGI GUIs SUMMARY SEE ALSO AUTHOR CONTRIBUTORS
perldebtut - Perl debugging tutorial
A (very) lightweight introduction in the use of the perl debugger, and a pointer to existing, deeper sources of information on the subject of debugging perl programs.
There's an extraordinary number of people out there who don't appear to know anything about using the perl debugger, though they use the language every day. This is for them.
First of all, there's a few things you can do to make your life a lot more straightforward when it comes to debugging perl programs, without using the debugger at all. To demonstrate, here's a simple script with a
$var1 = 'Hello World'; # always wanted to do that :-)
$var2 =
print $var2;
exit;
$var1 = 'Hello World'
$varl = undef $var2 = To catch this kind of problem, we can force each variable to be declared before use by pulling in the strict module, by putting 'use strict;' after the first line of the script.
Now when you run it, perl complains about the 3 undeclared variables and we get four error messages because one
use strict;
my $var1 = 'Hello World';
my $varl = ''; my $var2 =
print $var2;
exit; We then do (always a good idea) a syntax check before we try to run it again:
And now when we run it, we get ``n'' still, but at least we know why. Just getting this script to compile has exposed the '$varl' (with the letter 'l) variable, and simply changing $varl to $var1 solves the problem.
Ok, but how about when you want to really see your data, what's in that dynamic variable, just before using it?
/usr/bin/perl
use strict;
my $key = 'welcome';
my %data = ( 'this' =
Looks OK , after it's been through the syntax check (perl -c scriptname), we run it and all we get is a blank line again! Hmmmm.
One common debugging approach here, would be to liberally sprinkle a few print statements, to add a check just before
done: ''
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::(./data:4): my $key = 'welcome';
Now, what we've done here is to launch the built-in perl debugger on our script. It's stopped at the first line of executable code and is waiting for input.
Before we go any further, you'll want to know how to quit the debugger: use just the letter 'q', not the words
DB
That's it, you're back on home turf again.
Fire the debugger up again on your script and we'll look at the help menu. There's a couple of ways of calling help: a simple 'h' will get you a long scrolled list of help, 'h' (pipe-h) will pipe the help through your pager ('more' or 'less' probably), and finally, 'h h' (h-space-h) will give you a helpful mini-screen
More confusing options than you can shake a big stick at! It's not as bad as it looks and it's very useful to know more about all of it, and fun too!
There's a couple of useful ones to know about straight away. You wouldn't think we're using any libraries at all at the moment, but 'v' will show which modules are currently loaded, by the debugger as well your script. 'V' and 'X' show variables in the program by package scope and can be constrained by pattern. 'm' shows methods and 'S' shows all subroutines (by
DB
DM
DB
DB
DB
DB
DB
DB
DB
DB
DB DB
DB
DB
The reference is truly dumped and we can finally see what we're dealing with. Our quoting was perfectly valid but wrong for our purposes, with 'and jerry' being treated as 2 separate words rather than a phrase, thus throwing the evenly paired hash structure out of alignment.
The '-w' switch would have told us about this, had we used it at the start, and saved us a lot of trouble:
We fix our quoting: 'tom' =
While we're here, take a closer look at the 'x' command, it's really useful and will merrily dump out nested references, complete objects, partial objects - just about whatever you throw at it:
Let's make a quick object and x-plode it, first we'll start the the debugger: it wants some form of input from STDIN , so we give it something non-commital,
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::(-e:1): 0
DB
DB
DB DB
DB
DB
For more on references see perlref and perlreftut
Here's a simple program which converts between Celsius and
use strict;
my $arg = $ARGV[0? '-c20'; if ($arg = /^-(cf)((-+)*d+(.d+)*)$/) {
my ($deg, $num) = ($1, $2); my ($in, $out) = ($num, $num); if ($deg eq 'c') { $deg = 'f'; $out =
sub f2c {
my $f = shift; my $c = 5 * $f - 32 / 9; return $c; }
sub c2f {
my $c = shift; my $f = 9 * $c / 5 + 32; return $f; } For some reason, the Fahrenheit to Celsius conversion fails to return the expected output. This is what it does:
Not very consistent! We'll set a breakpoint in the code manually and run it under the debugger to see what's going on. A breakpoint is a flag, to which the debugger will run without interruption, when it reaches the breakpoint, it will stop execution and offer a prompt for further interaction. In normal use, these debugger commands are completely ignored, and they are safe - if a little messy, to leave in production code.
my ($in, $out) = ($num, $num);
$DB::single=2; # insert at line 9! if ($deg eq 'c') ...
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::(temp:4): my $arg = $ARGV[0? '-c100';
DB
DB
DB
DB
DB
Note that to delete a breakpoint you use 'd' or 'D'.
Now we'll continue down into our subroutine, this time rather than by line number, we'll use the subroutine name,
DB DB
DB
DB
DB DB DB DB
DB DB
DB
A quick fix to the offending line (insert the missing parentheses) in the actual program and we're finished.
Actions, watch variables, stack traces etc.: on the TODO list.
a W t T
Ever wanted to know what a regex looked like? You'll need perl compiled with the DEBUGGING flag for
EXECUTING... Freeing REx: `^pe(a)*rl$'
Did you really want to know? :-) For more gory details on getting regular expressions to work, have a look at perlre, perlretut, and to decode the mysterious labels ( BOL and CURLYN , etc. above), see perldebguts.
To get all the output from your error log, and not miss any messages via helpful operating system buffering, insert a
$=1;
tail -f $error_log
BEGIN { $SIG{DIE} = sub { require Carp; Carp::confess(@_) } }
Various useful techniques for the redirection of STDOUT and STDERR filehandles are explained in perlopentut and perlfaq8.
Just a quick hint here for all those CGI programmers who can't figure out how on earth to get past that 'waiting for input' prompt, when running their CGI script from the command-line, try something like this:
Of course CGI and perlfaq9 will tell you more.
The command line interface is tightly integrated with an emacs extension and there's a vi interface too.
You don't have to do this all on the command line, though, there are a few GUI options out there. The nice thing about these is you can wave a mouse over a variable and a dump of it's data will appear in an appropriate window, or in a popup balloon, no more tiresome typing of 'x $varname' :-)
In particular have a hunt around for the following:
ptkdb perlTK based wrapper for the built-in debugger
ddd data display debugger
!PerlDevKit? and !PerlBuilder? are NT specific
NB . (more info on these and others would be appreciated).
We've seen how to encourage good coding practices with use strict and -w. We can run the perl debugger perl -d scriptname to inspect your data from within the perl debugger with the p and x commands. You can walk through your code, set breakpoints with b and step through that code with s or n, continue with c and return from a sub with r. Fairly intuitive stuff when you get down to it.
There is of course lots more to find out about, this has just scratched the surface. The best way to learn more is to use perldoc to find out more about the language, to read the on-line help (perldebug is probably the next place to go), and of course, experiment.
perldebug, perldebguts, perldiag, dprofpp, perlrun
Richard Foley
Various people have made helpful suggestions and contributions, in particular:
Ronald J Kimball
Hugo van der Sanden
Peter Scott
One page links to perldebtut(1):