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