Penguin
Blame: perldebtut(1)
EditPageHistoryDiffInfoLikePages
Annotated edit history of perldebtut(1) version 2, including all changes. View license author blame.
Rev Author # Line
1 perry 1 PERLDEBTUT
2 !!!PERLDEBTUT
3 NAME
4 DESCRIPTION
5 use strict
6 Looking at data and -w and w
7 help
8 Stepping through code
9 Placeholder for a, w, t, T
10 REGULAR EXPRESSIONS
11 OUTPUT TIPS
12 CGI
13 GUIs
14 SUMMARY
15 SEE ALSO
16 AUTHOR
17 CONTRIBUTORS
18 ----
19 !!NAME
20
21
22 perldebtut - Perl debugging tutorial
23 !!DESCRIPTION
24
25
26 A (very) lightweight introduction in the use of the perl
27 debugger, and a pointer to existing, deeper sources of
28 information on the subject of debugging perl
29 programs.
30
31
32 There's an extraordinary number of people out there who
33 don't appear to know anything about using the perl debugger,
34 though they use the language every day. This is for
35 them.
36 !!use strict
37
38
39 First of all, there's a few things you can do to make your
40 life a lot more straightforward when it comes to debugging
41 perl programs, without using the debugger at all. To
42 demonstrate, here's a simple script with a
43 problem:
44
45
46 #!/usr/bin/perl
47 $var1 = 'Hello World'; # always wanted to do that :-)
48 $var2 =
49 print $var2;
50 exit;
51 While this compiles and runs happily, it probably won't do what's expected, namely it doesn't print ``Hello Worldn'' at all; It will on the other hand do exactly what it was told to do, computers being a bit that way inclined. That is, it will print out a newline character, and you'll get what looks like a blank line. It looks like there's 2 variables when (because of the typo) there's really 3:
52
53
54 $var1 = 'Hello World'
55 $varl = undef
56 $var2 =
57 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.
58
59
60 Now when you run it, perl complains about the 3 undeclared
61 variables and we get four error messages because one
62 variable is referenced twice:
63
64
65 Global symbol
66 Luvverly! and to fix this we declare all variables explicitly and now our script looks like this:
67
68
69 #!/usr/bin/perl
70 use strict;
71 my $var1 = 'Hello World';
72 my $varl = '';
73 my $var2 =
74 print $var2;
75 exit;
76 We then do (always a good idea) a syntax check before we try to run it again:
77
78
79
80 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.
81 !!Looking at data and -w and w
82
83
84 Ok, but how about when you want to really see your data,
85 what's in that dynamic variable, just before using
86 it?
87
88
89 #!/usr/bin/perl
90 use strict;
91 my $key = 'welcome';
92 my %data = (
93 'this' =
94 print
95 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.
96
97
98 One common debugging approach here, would be to liberally
99 sprinkle a few print statements, to add a check just before
100 we print out our data, and another just after:
101
102
103 print
104 And try again:
105
106
107
108 done: ''
109 After much staring at the same piece of code and not seeing the wood for the trees for some time, we get a cup of coffee and try another approach. That is, we bring in the cavalry by giving perl the '__-d__' switch on the command line:
110
111
112
113 Loading DB routines from perl5db.pl version 1.07
114 Editor support available.
115 Enter h or `h h' for help, or `man perldebug' for more help.
116 main::(./data:4): my $key = 'welcome';
117 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.
118
119
120 Before we go any further, you'll want to know how to quit
121 the debugger: use just the letter '__q__', not the words
122 'quit' or 'exit':
123
124
125 DB
126 That's it, you're back on home turf again.
127 !!help
128
129
130 Fire the debugger up again on your script and we'll look at
131 the help menu. There's a couple of ways of calling help: a
132 simple '__h__' will get you a long scrolled list of help,
133 '__h__' (pipe-h) will pipe the help through your pager
134 ('more' or 'less' probably), and finally, '__h h__'
135 (h-space-h) will give you a helpful mini-screen
136 snapshot:
137
138
139 DB
140 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!
141
142
143 There's a couple of useful ones to know about straight away.
144 You wouldn't think we're using any libraries at all at the
145 moment, but '__v__' will show which modules are currently
146 loaded, by the debugger as well your script. '__V__' and
147 '__X__' show variables in the program by package scope
148 and can be constrained by pattern. '__m__' shows methods
149 and '__S__' shows all subroutines (by
150 pattern):
151
152
153 DB
154 Using 'X' and cousins requires you not to use the type identifiers ($@%), just the 'name':
155
156
157 DM
158 Remember we're in our tiny program with a problem, we should have a look at where we are, and what our data looks like. First of all let's have a window on our present position (the first line of code in this case), via the letter '__w__':
159
160
161 DB
162 At line number 4 is a helpful pointer, that tells you where you are now. To see more code, type 'w' again:
163
164
165 DB
166 And if you wanted to list line 5 again, type 'l 5', (note the space):
167
168
169 DB
170 In this case, there's not much to see, but of course normally there's pages of stuff to wade through, and 'l' can be very useful. To reset your view to the line we're about to execute, type a lone period '.':
171
172
173 DB
174 The line shown is the one that is about to be executed __next__, it hasn't happened yet. So while we can print a variable with the letter '__p__', at this point all we'd get is an empty (undefined) value back. What we need to do is to step through the next executable statement with an '__s__':
175
176
177 DB
178 Now we can have a look at that first ($key) variable:
179
180
181 DB
182 line 13 is where the action is, so let's continue down to there via the letter '__c__', which by the way, inserts a 'one-time-only' breakpoint at the given line or sub routine:
183
184
185 DB
186 We've gone past our check (where 'All OK ' was printed) and have stopped just before the meat of our task. We could try to print out a couple of variables to see what is happening:
187
188
189 DB
190 Not much in there, lets have a look at our hash:
191
192
193 DB
194 DB
195 Well, this isn't very easy to read, and using the helpful manual (__h h__), the '__x__' command looks promising:
196
197
198 DB
199 That's not much help, a couple of welcomes in there, but no indication of which are keys, and which are values, it's just a listed array dump and, in this case, not particularly helpful. The trick here, is to use a __reference__ to the data structure:
200
201
202 DB
203 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.
204
205
206 The '__-w__' switch would have told us about this, had we
207 used it at the start, and saved us a lot of
208 trouble:
209
210
211
212 We fix our quoting: 'tom' =
213
214
215
216 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:
217
218
219 Let's make a quick object and x-plode it, first we'll start
220 the the debugger: it wants some form of input from
221 STDIN , so we give it something non-commital,
222 a zero:
223
224
225
226 Loading DB routines from perl5db.pl version 1.07
227 Editor support available.
228 Enter h or `h h' for help, or `man perldebug' for more help.
229 main::(-e:1): 0
230 Now build an on-the-fly object over a couple of lines (note the backslash):
231
232
233 DB
234 And let's have a look at it:
235
236
237 DB
238 Useful, huh? You can eval nearly anything in there, and experiment with bits of code or regexes until the cows come home:
239
240
241 DB
242 DB
243 If you want to see the command History, type an '__H__':
244
245
246 DB
247 And if you want to repeat any previous command, use the exclamation: '__!__':
248
249
250 DB
251 For more on references see perlref and perlreftut
252 !!Stepping through code
253
254
255 Here's a simple program which converts between Celsius and
256 Fahrenheit, it too has a problem:
257
258
259 #!/usr/bin/perl -w
260 use strict;
261 my $arg = $ARGV[[0] '-c20';
262 if ($arg =~ /^-(cf)((-+)*d+(.d+)*)$/) {
263 my ($deg, $num) = ($1, $2);
264 my ($in, $out) = ($num, $num);
265 if ($deg eq 'c') {
266 $deg = 'f';
267 $out =
268 sub f2c {
269 my $f = shift;
270 my $c = 5 * $f - 32 / 9;
271 return $c;
272 }
273 sub c2f {
274 my $c = shift;
275 my $f = 9 * $c / 5 + 32;
276 return $f;
277 }
278 For some reason, the Fahrenheit to Celsius conversion fails to return the expected output. This is what it does:
279
280
281
282
283 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.
284
285
286 my ($in, $out) = ($num, $num);
287 $DB::single=2; # insert at line 9!
288 if ($deg eq 'c')
289 ...
290
291 Loading DB routines from perl5db.pl version 1.07
292 Editor support available.
293 Enter h or `h h' for help, or `man perldebug' for more help.
294 main::(temp:4): my $arg = $ARGV[[0] '-c100';
295 We'll simply continue down to our pre-set breakpoint with a '__c__':
296
297
298 DB
299 Followed by a window command to see where we are:
300
301
302 DB
303 And a print to show what values we're currently using:
304
305
306 DB
307 We can put another break point on any line beginning with a colon, we'll use line 17 as that's just as we come out of the subroutine, and we'd like to pause there later on:
308
309
310 DB
311 There's no feedback from this, but you can see what breakpoints are set by using the list 'L' command:
312
313
314 DB
315 Note that to delete a breakpoint you use 'd' or 'D'.
316
317
318 Now we'll continue down into our subroutine, this time
319 rather than by line number, we'll use the subroutine name,
320 followed by the now familiar 'w':
321
322
323 DB
324 DB
325 Note that if there was a subroutine call between us and line 29, and we wanted to __single-step__ through it, we could use the '__s__' command, and to step over it we would use '__n__' which would execute the sub, but not descend into it for inspection. In this case though, we simply continue down to line 29:
326
327
328 DB
329 And have a look at the return value:
330
331
332 DB
333 This is not the right answer at all, but the sum looks correct. I wonder if it's anything to do with operator precedence? We'll try a couple of other possibilities with our sum:
334
335
336 DB
337 DB
338 DB
339 DB
340 :-) that's more like it! Ok, now we can set our return variable and we'll return out of the sub with an 'r':
341
342
343 DB
344 DB
345 Looks good, let's just continue off the end of the script:
346
347
348 DB
349 A quick fix to the offending line (insert the missing parentheses) in the actual program and we're finished.
350 !!Placeholder for a, w, t, T
351
352
353 Actions, watch variables, stack traces etc.: on the
354 TODO list.
355
356
357 a
358 W
359 t
360 T
361 !!REGULAR EXPRESSIONS
362
363
364 Ever wanted to know what a regex looked like? You'll need
365 perl compiled with the DEBUGGING flag for
366 this one:
367
368
369
370 EXECUTING...
371 Freeing REx: `^pe(a)*rl$'
372 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.
373 !!OUTPUT TIPS
374
375
376 To get all the output from your error log, and not miss any
377 messages via helpful operating system buffering, insert a
378 line like this, at the start of your script:
379
380
381 $=1;
382 To watch the tail of a dynamically growing logfile, (from the command line):
383
384
385 tail -f $error_log
386 Wrapping all die calls in a handler routine can be useful to see how, and from where, they're being called, perlvar has more information:
387
388
389 BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } }
390 Various useful techniques for the redirection of STDOUT and STDERR filehandles are explained in perlopentut and perlfaq8.
391 !!CGI
392
393
394 Just a quick hint here for all those CGI
395 programmers who can't figure out how on earth to get past
396 that 'waiting for input' prompt, when running their
397 CGI script from the command-line, try
398 something like this:
399
400
401
402 Of course CGI and perlfaq9 will tell you more.
403 !!GUIs
404
405
406 The command line interface is tightly integrated with an
407 __emacs__ extension and there's a __vi__ interface
408 too.
409
410
411 You don't have to do this all on the command line, though,
412 there are a few GUI options out there. The
413 nice thing about these is you can wave a mouse over a
414 variable and a dump of it's data will appear in an
415 appropriate window, or in a popup balloon, no more tiresome
416 typing of 'x $varname' :-)
417
418
419 In particular have a hunt around for the
420 following:
421
422
423 __ptkdb__ perlTK based wrapper for the built-in
424 debugger
425
426
427 __ddd__ data display debugger
428
429
2 perry 430 __!PerlDevKit__ and __!PerlBuilder__ are
1 perry 431 NT specific
432
433
434 NB . (more info on these and others would be
435 appreciated).
436 !!SUMMARY
437
438
439 We've seen how to encourage good coding practices with
440 __use strict__ and __-w__. We can run the perl
441 debugger __perl -d scriptname__ to inspect your data from
442 within the perl debugger with the __p__ and __x__
443 commands. You can walk through your code, set breakpoints
444 with __b__ and step through that code with __s__ or
445 __n__, continue with __c__ and return from a sub with
446 __r__. Fairly intuitive stuff when you get down to
447 it.
448
449
450 There is of course lots more to find out about, this has
451 just scratched the surface. The best way to learn more is to
452 use perldoc to find out more about the language, to read the
453 on-line help (perldebug is probably the next place to go),
454 and of course, experiment.
455 !!SEE ALSO
456
457
458 perldebug, perldebguts, perldiag, dprofpp,
459 perlrun
460 !!AUTHOR
461
462
463 Richard Foley
464 !!CONTRIBUTORS
465
466
467 Various people have made helpful suggestions and
468 contributions, in particular:
469
470
471 Ronald J Kimball
472
473
474 Hugo van der Sanden
475
476
477 Peter Scott
478 ----
This page is a man page (or other imported legacy content). We are unable to automatically determine the license status of this page.