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