Once again syncing after too long an absence
[p5sagit/p5-mst-13.2.git] / pod / perldebtut.pod
CommitLineData
22d4bb9c 1=head1 NAME
2
3perldebtut - Perl debugging tutorial
4
5=head1 DESCRIPTION
6
7A (very) lightweight introduction in the use of the perl debugger, and a
8pointer to existing, deeper sources of information on the subject of debugging
9perl programs.
10
11There's an extraordinary number of people out there who don't appear to know
12anything about using the perl debugger, though they use the language every
13day.
14This is for them.
15
16
17=head1 use strict
18
19First of all, there's a few things you can do to make your life a lot more
20straightforward when it comes to debugging perl programs, without using the
21debugger at all. To demonstrate, here's a simple script with a problem:
22
23 #!/usr/bin/perl
24
25 $var1 = 'Hello World'; # always wanted to do that :-)
26 $var2 = "$varl\n";
27
28 print $var2;
29 exit;
30
31While this compiles and runs happily, it probably won't do what's expected,
32namely it doesn't print "Hello World\n" at all; It will on the other hand do
33exactly what it was told to do, computers being a bit that way inclined. That
34is, it will print out a newline character, and you'll get what looks like a
35blank line. It looks like there's 2 variables when (because of the typo)
36there's really 3:
37
38 $var1 = 'Hello World'
39 $varl = undef
40 $var2 = "\n"
41
42To catch this kind of problem, we can force each variable to be declared
43before use by pulling in the strict module, by putting 'use strict;' after the
44first line of the script.
45
46Now when you run it, perl complains about the 3 undeclared variables and we
47get four error messages because one variable is referenced twice:
48
49 Global symbol "$var1" requires explicit package name at ./t1 line 4.
50 Global symbol "$var2" requires explicit package name at ./t1 line 5.
51 Global symbol "$varl" requires explicit package name at ./t1 line 5.
52 Global symbol "$var2" requires explicit package name at ./t1 line 7.
53 Execution of ./hello aborted due to compilation errors.
54
55Luvverly! and to fix this we declare all variables explicitly and now our
56script looks like this:
57
58 #!/usr/bin/perl
59 use strict;
60
61 my $var1 = 'Hello World';
62 my $varl = '';
63 my $var2 = "$varl\n";
64
65 print $var2;
66 exit;
67
68We then do (always a good idea) a syntax check before we try to run it again:
69
70 > perl -c hello
71 hello syntax OK
72
73And now when we run it, we get "\n" still, but at least we know why. Just
74getting this script to compile has exposed the '$varl' (with the letter 'l)
75variable, and simply changing $varl to $var1 solves the problem.
76
77
78=head1 Looking at data and -w and w
79
80Ok, but how about when you want to really see your data, what's in that
81dynamic variable, just before using it?
82
83 #!/usr/bin/perl
84 use strict;
85
86 my $key = 'welcome';
87 my %data = (
88 'this' => qw(that),
89 'tom' => qw(and jerry),
90 'welcome' => q(Hello World),
91 'zip' => q(welcome),
92 );
93 my @data = keys %data;
94
95 print "$data{$key}\n";
96 exit;
97
98Looks OK, after it's been through the syntax check (perl -c scriptname), we
99run it and all we get is a blank line again! Hmmmm.
100
101One common debugging approach here, would be to liberally sprinkle a few print
102statements, to add a check just before we print out our data, and another just
103after:
104
105 print "All OK\n" if grep($key, keys %data);
106 print "$data{$key}\n";
107 print "done: '$data{$key}'\n";
108
109And try again:
110
111 > perl data
112 All OK
113
114 done: ''
115
116After much staring at the same piece of code and not seeing the wood for the
117trees for some time, we get a cup of coffee and try another approach. That
118is, we bring in the cavalry by giving perl the 'B<-d>' switch on the command
119line:
120
121 > perl -d data
122 Default die handler restored.
123
124 Loading DB routines from perl5db.pl version 1.07
125 Editor support available.
126
127 Enter h or `h h' for help, or `man perldebug' for more help.
128
129 main::(./data:4): my $key = 'welcome';
130
131Now, what we've done here is to launch the built-in perl debugger on our
132script. It's stopped at the first line of executable code and is waiting for
133input.
134
135Before we go any further, you'll want to know how to quit the debugger: use
136just the letter 'B<q>', not the words 'quit' or 'exit':
137
138 DB<1> q
139 >
140
141That's it, you're back on home turf again.
142
143
144=head1 help
145
146Fire the debugger up again on your script and we'll look at the help menu.
147There's a couple of ways of calling help: a simple 'B<h>' will get you a long
148scrolled list of help, 'B<|h>' (pipe-h) will pipe the help through your pager
149('more' or 'less' probably), and finally, 'B<h h>' (h-space-h) will give you a
150helpful mini-screen snapshot:
151
152 DB<1> h h
153 List/search source lines: Control script execution:
154 l [ln|sub] List source code T Stack trace
155 - or . List previous/current line s [expr] Single step [in expr]
156 w [line] List around line n [expr] Next, steps over subs
157 f filename View source in file <CR/Enter> Repeat last n or s
158 /pattern/ ?patt? Search forw/backw r Return from subroutine
159 v Show versions of modules c [ln|sub] Continue until position
160 Debugger controls: L List
161break/watch/actions
162 O [...] Set debugger options t [expr] Toggle trace [trace expr]
163 <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
164 ! [N|pat] Redo a previous command d [ln] or D Delete a/all breakpoints
165 H [-num] Display last num commands a [ln] cmd Do cmd before line
166 = [a val] Define/list an alias W expr Add a watch expression
167 h [db_cmd] Get help on command A or W Delete all actions/watch
168 |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess
169 q or ^D Quit R Attempt a restart
170 Data Examination: expr Execute perl code, also see: s,n,t expr
171 x|m expr Evals expr in list context, dumps the result or lists methods.
172 p expr Print expression (uses script's current package).
173 S [[!]pat] List subroutine names [not] matching pattern
174 V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
175 X [Vars] Same as "V current_package [Vars]".
176 For more help, type h cmd_letter, or run man perldebug for all docs.
177
178More confusing options than you can shake a big stick at! It's not as bad as
179it looks and it's very useful to know more about all of it, and fun too!
180
181There's a couple of useful ones to know about straight away. You wouldn't
182think we're using any libraries at all at the moment, but 'B<v>' will show
183which modules are currently loaded, by the debugger as well your script.
184'B<V>' and 'B<X>' show variables in the program by package scope and can be
185constrained by pattern. 'B<m>' shows methods and 'B<S>' shows all subroutines
186(by pattern):
187
188 DB<2>S str
189 dumpvar::stringify
190 strict::bits
191 strict::import
192 strict::unimport
193
194Using 'X' and cousins requires you not to use the type identifiers ($@%), just
195the 'name':
196
197 DM<3>X ~err
198 FileHandle(stderr) => fileno(2)
199
200Remember we're in our tiny program with a problem, we should have a look at
201where we are, and what our data looks like. First of all let's have a window
202on our present position (the first line of code in this case), via the letter
203'B<w>':
204
205 DB<4> w
206 1 #!/usr/bin/perl
207 2: use strict;
208 3
209 4==> my $key = 'welcome';
210 5: my %data = (
211 6 'this' => qw(that),
212 7 'tom' => qw(and jerry),
213 8 'welcome' => q(Hello World),
214 9 'zip' => q(welcome),
215 10 );
216
217At line number 4 is a helpful pointer, that tells you where you are now. To
218see more code, type 'w' again:
219
220 DB<4> w
221 8 'welcome' => q(Hello World),
222 9 'zip' => q(welcome),
223 10 );
224 11: my @data = keys %data;
225 12: print "All OK\n" if grep($key, keys %data);
226 13: print "$data{$key}\n";
227 14: print "done: '$data{$key}'\n";
228 15: exit;
229
230And if you wanted to list line 5 again, type 'l 5', (note the space):
231
232 DB<4> l 5
233 5: my %data = (
234
235In this case, there's not much to see, but of course normally there's pages of
236stuff to wade through, and 'l' can be very useful. To reset your view to the
237line we're about to execute, type a lone period '.':
238
239 DB<5> .
240 main::(./data_a:4): my $key = 'welcome';
241
242The line shown is the one that is about to be executed B<next>, it hasn't
243happened yet. So while we can print a variable with the letter 'B<p>', at
244this point all we'd get is an empty (undefined) value back. What we need to
245do is to step through the next executable statement with an 'B<s>':
246
247 DB<6> s
248 main::(./data_a:5): my %data = (
249 main::(./data_a:6): 'this' => qw(that),
250 main::(./data_a:7): 'tom' => qw(and jerry),
251 main::(./data_a:8): 'welcome' => q(Hello World),
252 main::(./data_a:9): 'zip' => q(welcome),
253 main::(./data_a:10): );
254
255Now we can have a look at that first ($key) variable:
256
257 DB<7> p $key
258 welcome
259
260line 13 is where the action is, so let's continue down to there via the letter
261'B<c>', which by the way, inserts a 'one-time-only' breakpoint at the given
262line or sub routine:
263
264 DB<8> c 13
265 All OK
266 main::(./data_a:13): print "$data{$key}\n";
267
268We've gone past our check (where 'All OK' was printed) and have stopped just
269before the meat of our task. We could try to print out a couple of variables
270to see what is happening:
271
272 DB<9> p $data{$key}
273
274Not much in there, lets have a look at our hash:
275
276 DB<10> p %data
277 Hello Worldziptomandwelcomejerrywelcomethisthat
278
279 DB<11> p keys %data
280 Hello Worldtomwelcomejerrythis
281
282Well, this isn't very easy to read, and using the helpful manual (B<h h>), the
283'B<x>' command looks promising:
284
285 DB<12> x %data
286 0 'Hello World'
287 1 'zip'
288 2 'tom'
289 3 'and'
290 4 'welcome'
291 5 undef
292 6 'jerry'
293 7 'welcome'
294 8 'this'
295 9 'that'
296
297That's not much help, a couple of welcomes in there, but no indication of
298which are keys, and which are values, it's just a listed array dump and, in
299this case, not particularly helpful. The trick here, is to use a B<reference>
300to the data structure:
301
302 DB<13> x \%data
303 0 HASH(0x8194bc4)
304 'Hello World' => 'zip'
305 'jerry' => 'welcome'
306 'this' => 'that'
307 'tom' => 'and'
308 'welcome' => undef
309
310The reference is truly dumped and we can finally see what we're dealing with.
311Our quoting was perfectly valid but wrong for our purposes, with 'and jerry'
312being treated as 2 separate words rather than a phrase, thus throwing the
313evenly paired hash structure out of alignment.
314
315The 'B<-w>' switch would have told us about this, had we used it at the start,
316and saved us a lot of trouble:
317
318 > perl -w data
319 Odd number of elements in hash assignment at ./data line 5.
320
321We fix our quoting: 'tom' => q(and jerry), and run it again, this time we get
322our expected output:
323
324 > perl -w data
325 Hello World
326
327
328While we're here, take a closer look at the 'B<x>' command, it's really useful
329and will merrily dump out nested references, complete objects, partial objects
330- just about whatever you throw at it:
331
332Let's make a quick object and x-plode it, first we'll start the the debugger:
333it wants some form of input from STDIN, so we give it something non-commital,
334a zero:
335
336 > perl -de 0
337 Default die handler restored.
338
339 Loading DB routines from perl5db.pl version 1.07
340 Editor support available.
341
342 Enter h or `h h' for help, or `man perldebug' for more help.
343
344 main::(-e:1): 0
345
346Now build an on-the-fly object over a couple of lines (note the backslash):
347
348 DB<1> $obj = bless({'unique_id'=>'123', 'attr'=> \
349 cont: {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
350
351And let's have a look at it:
352
353 DB<2> x $obj
354 0 MY_class=HASH(0x828ad98)
355 'attr' => HASH(0x828ad68)
356 'col' => 'black'
357 'things' => ARRAY(0x828abb8)
358 0 'this'
359 1 'that'
360 2 'etc'
361 'unique_id' => 123
362 DB<3>
363
364Useful, huh? You can eval nearly anything in there, and experiment with bits
365of code or regexes until the cows come home:
366
367 DB<3> @data = qw(this that the other atheism leather theory scythe)
368
369 DB<4> p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
370 atheism
371 leather
372 other
373 scythe
374 the
375 theory
376 saw -> 6
377
378If you want to see the command History, type an 'B<H>':
379
380 DB<5> H
381 4: p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
382 3: @data = qw(this that the other atheism leather theory scythe)
383 2: x $obj
384 1: $obj = bless({'unique_id'=>'123', 'attr'=>
385 {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
386 DB<5>
387
388And if you want to repeat any previous command, use the exclamation: 'B<!>':
389
390 DB<5> !4
391 p 'saw -> '.($cnt += map { print "$_\n" } grep(/the/, sort @data))
392 atheism
393 leather
394 other
395 scythe
396 the
397 theory
398 saw -> 12
399
400For more on references see L<perlref> and L<perlreftut>
401
402
403=head1 Stepping through code
404
405Here's a simple program which converts between Celsius and Fahrenheit, it too
406has a problem:
407
408 #!/usr/bin/perl -w
409 use strict;
410
411 my $arg = $ARGV[0] || '-c20';
412
413 if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) {
414 my ($deg, $num) = ($1, $2);
415 my ($in, $out) = ($num, $num);
416 if ($deg eq 'c') {
417 $deg = 'f';
418 $out = &c2f($num);
419 } else {
420 $deg = 'c';
421 $out = &f2c($num);
422 }
423 $out = sprintf('%0.2f', $out);
424 $out =~ s/^((\-|\+)*\d+)\.0+$/$1/;
425 print "$out $deg\n";
426 } else {
427 print "Usage: $0 -[c|f] num\n";
428 }
429 exit;
430
431 sub f2c {
432 my $f = shift;
433 my $c = 5 * $f - 32 / 9;
434 return $c;
435 }
436
437 sub c2f {
438 my $c = shift;
439 my $f = 9 * $c / 5 + 32;
440 return $f;
441 }
442
443
444For some reason, the Fahrenheit to Celsius conversion fails to return the
445expected output. This is what it does:
446
447 > temp -c0.72
448 33.30 f
449
450 > temp -f33.3
451 162.94 c
452
453Not very consistent! We'll set a breakpoint in the code manually and run it
454under the debugger to see what's going on. A breakpoint is a flag, to which
455the debugger will run without interruption, when it reaches the breakpoint, it
456will stop execution and offer a prompt for further interaction. In normal
457use, these debugger commands are completely ignored, and they are safe - if a
458little messy, to leave in production code.
459
460 my ($in, $out) = ($num, $num);
461 $DB::single=2; # insert at line 9!
462 if ($deg eq 'c')
463 ...
464
465 > perl -d temp -f33.3
466 Default die handler restored.
467
468 Loading DB routines from perl5db.pl version 1.07
469 Editor support available.
470
471 Enter h or `h h' for help, or `man perldebug' for more help.
472
473 main::(temp:4): my $arg = $ARGV[0] || '-c100';
474
475We'll simply continue down to our pre-set breakpoint with a 'B<c>':
476
477 DB<1> c
478 main::(temp:10): if ($deg eq 'c') {
479
480Followed by a window command to see where we are:
481
482 DB<1> w
483 7: my ($deg, $num) = ($1, $2);
484 8: my ($in, $out) = ($num, $num);
485 9: $DB::single=2;
486 10==> if ($deg eq 'c') {
487 11: $deg = 'f';
488 12: $out = &c2f($num);
489 13 } else {
490 14: $deg = 'c';
491 15: $out = &f2c($num);
492 16 }
493
494And a print to show what values we're currently using:
495
496 DB<1> p $deg, $num
497 f33.3
0e06870b 498
22d4bb9c 499We can put another break point on any line beginning with a colon, we'll use
500line 17 as that's just as we come out of the subroutine, and we'd like to
501pause there later on:
502
503 DB<2> b 17
504
505There's no feedback from this, but you can see what breakpoints are set by
506using the list 'L' command:
507
508 DB<3> L
509 temp:
510 17: print "$out $deg\n";
511 break if (1)
512
513Note that to delete a breakpoint you use 'd' or 'D'.
514
515Now we'll continue down into our subroutine, this time rather than by line
516number, we'll use the subroutine name, followed by the now familiar 'w':
517
518 DB<3> c f2c
519 main::f2c(temp:30): my $f = shift;
520
521 DB<4> w
522 24: exit;
523 25
524 26 sub f2c {
525 27==> my $f = shift;
526 28: my $c = 5 * $f - 32 / 9;
527 29: return $c;
528 30 }
529 31
530 32 sub c2f {
531 33: my $c = shift;
532
533
534Note that if there was a subroutine call between us and line 29, and we wanted
535to B<single-step> through it, we could use the 'B<s>' command, and to step
536over it we would use 'B<n>' which would execute the sub, but not descend into
537it for inspection. In this case though, we simply continue down to line 29:
538
539 DB<4> c 29
540 main::f2c(temp:29): return $c;
0e06870b 541
22d4bb9c 542And have a look at the return value:
543
544 DB<5> p $c
545 162.944444444444
546
547This is not the right answer at all, but the sum looks correct. I wonder if
548it's anything to do with operator precedence? We'll try a couple of other
549possibilities with our sum:
550
551 DB<6> p (5 * $f - 32 / 9)
552 162.944444444444
553
554 DB<7> p 5 * $f - (32 / 9)
555 162.944444444444
556
557 DB<8> p (5 * $f) - 32 / 9
558 162.944444444444
559
560 DB<9> p 5 * ($f - 32) / 9
561 0.722222222222221
562
563:-) that's more like it! Ok, now we can set our return variable and we'll
564return out of the sub with an 'r':
565
566 DB<10> $c = 5 * ($f - 32) / 9
567
568 DB<11> r
569 scalar context return from main::f2c: 0.722222222222221
570
571Looks good, let's just continue off the end of the script:
572
573 DB<12> c
574 0.72 c
575 Debugged program terminated. Use q to quit or R to restart,
576 use O inhibit_exit to avoid stopping after program termination,
577 h q, h R or h O to get additional info.
578
579A quick fix to the offending line (insert the missing parentheses) in the
580actual program and we're finished.
581
582
583=head1 Placeholder for a, w, t, T
584
585Actions, watch variables, stack traces etc.: on the TODO list.
586
587 a
588
589 W
590
591 t
592
593 T
594
595
596=head1 REGULAR EXPRESSIONS
597
598Ever wanted to know what a regex looked like? You'll need perl compiled with
599the DEBUGGING flag for this one:
600
601 > perl -Dr -e '/^pe(a)*rl$/i'
602 Compiling REx `^pe(a)*rl$'
603 size 17 first at 2
604 rarest char
605 at 0
606 1: BOL(2)
607 2: EXACTF <pe>(4)
608 4: CURLYN[1] {0,32767}(14)
609 6: NOTHING(8)
610 8: EXACTF <a>(0)
611 12: WHILEM(0)
612 13: NOTHING(14)
613 14: EXACTF <rl>(16)
614 16: EOL(17)
615 17: END(0)
616 floating `'$ at 4..2147483647 (checking floating) stclass `EXACTF <pe>'
617anchored(BOL) minlen 4
618 Omitting $` $& $' support.
0e06870b 619
22d4bb9c 620 EXECUTING...
621
622 Freeing REx: `^pe(a)*rl$'
623
624Did you really want to know? :-)
625For more gory details on getting regular expressions to work, have a look at
626L<perlre>, L<perlretut>, and to decode the mysterious labels (BOL and CURLYN,
627etc. above), see L<perldebguts>.
628
629
630=head1 OUTPUT TIPS
631
632To get all the output from your error log, and not miss any messages via
633helpful operating system buffering, insert a line like this, at the start of
634your script:
635
636 $|=1;
637
638To watch the tail of a dynamically growing logfile, (from the command line):
639
640 tail -f $error_log
641
642Wrapping all die calls in a handler routine can be useful to see how, and from
643where, they're being called, L<perlvar> has more information:
644
645 BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } }
646
647Various useful techniques for the redirection of STDOUT and STDERR filehandles
648are explained in L<perlopentut> and L<perlfaq8>.
649
650
651=head1 CGI
652
653Just a quick hint here for all those CGI programmers who can't figure out how
654on earth to get past that 'waiting for input' prompt, when running their CGI
655script from the command-line, try something like this:
656
657 > perl -d my_cgi.pl -nodebug
658
0e06870b 659Of course L<CGI> and L<perlfaq9> will tell you more.
22d4bb9c 660
661
662=head1 GUIs
663
664The command line interface is tightly integrated with an B<emacs> extension
665and there's a B<vi> interface too.
666
667You don't have to do this all on the command line, though, there are a few GUI
668options out there. The nice thing about these is you can wave a mouse over a
669variable and a dump of it's data will appear in an appropriate window, or in a
670popup balloon, no more tiresome typing of 'x $varname' :-)
671
672In particular have a hunt around for the following:
673
674B<ptkdb> perlTK based wrapper for the built-in debugger
675
676B<ddd> data display debugger
677
678B<PerlDevKit> and B<PerlBuilder> are NT specific
679
680NB. (more info on these and others would be appreciated).
681
682
683=head1 SUMMARY
684
685We've seen how to encourage good coding practices with B<use strict> and
686B<-w>. We can run the perl debugger B<perl -d scriptname> to inspect your
687data from within the perl debugger with the B<p> and B<x> commands. You can
688walk through your code, set breakpoints with B<b> and step through that code
689with B<s> or B<n>, continue with B<c> and return from a sub with B<r>. Fairly
690intuitive stuff when you get down to it.
691
692There is of course lots more to find out about, this has just scratched the
693surface. The best way to learn more is to use perldoc to find out more about
694the language, to read the on-line help (L<perldebug> is probably the next
695place to go), and of course, experiment.
696
697
698=head1 SEE ALSO
699
700L<perldebug>,
701L<perldebguts>,
702L<perldiag>,
703L<dprofpp>,
704L<perlrun>
705
706
707=head1 AUTHOR
708
709Richard Foley <richard@rfi.net> Copyright (c) 2000
710
711
712=head1 CONTRIBUTORS
713
714Various people have made helpful suggestions and contributions, in particular:
715
716Ronald J Kimball <rjk@linguist.dartmouth.edu>
717
718Hugo van der Sanden <hv@crypt0.demon.co.uk>
719
720Peter Scott <Peter@PSDT.com>
721