Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / man / man1 / perldebtut.1
CommitLineData
920dae64
AT
1.\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32
2.\"
3.\" Standard preamble:
4.\" ========================================================================
5.de Sh \" Subsection heading
6.br
7.if t .Sp
8.ne 5
9.PP
10\fB\\$1\fR
11.PP
12..
13.de Sp \" Vertical space (when we can't use .PP)
14.if t .sp .5v
15.if n .sp
16..
17.de Vb \" Begin verbatim text
18.ft CW
19.nf
20.ne \\$1
21..
22.de Ve \" End verbatim text
23.ft R
24.fi
25..
26.\" Set up some character translations and predefined strings. \*(-- will
27.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left
28.\" double quote, and \*(R" will give a right double quote. | will give a
29.\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to
30.\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C'
31.\" expand to `' in nroff, nothing in troff, for use with C<>.
32.tr \(*W-|\(bv\*(Tr
33.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'
34.ie n \{\
35. ds -- \(*W-
36. ds PI pi
37. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch
38. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch
39. ds L" ""
40. ds R" ""
41. ds C` ""
42. ds C' ""
43'br\}
44.el\{\
45. ds -- \|\(em\|
46. ds PI \(*p
47. ds L" ``
48. ds R" ''
49'br\}
50.\"
51.\" If the F register is turned on, we'll generate index entries on stderr for
52.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index
53.\" entries marked with X<> in POD. Of course, you'll have to process the
54.\" output yourself in some meaningful fashion.
55.if \nF \{\
56. de IX
57. tm Index:\\$1\t\\n%\t"\\$2"
58..
59. nr % 0
60. rr F
61.\}
62.\"
63.\" For nroff, turn off justification. Always turn off hyphenation; it makes
64.\" way too many mistakes in technical documents.
65.hy 0
66.if n .na
67.\"
68.\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2).
69.\" Fear. Run. Save yourself. No user-serviceable parts.
70. \" fudge factors for nroff and troff
71.if n \{\
72. ds #H 0
73. ds #V .8m
74. ds #F .3m
75. ds #[ \f1
76. ds #] \fP
77.\}
78.if t \{\
79. ds #H ((1u-(\\\\n(.fu%2u))*.13m)
80. ds #V .6m
81. ds #F 0
82. ds #[ \&
83. ds #] \&
84.\}
85. \" simple accents for nroff and troff
86.if n \{\
87. ds ' \&
88. ds ` \&
89. ds ^ \&
90. ds , \&
91. ds ~ ~
92. ds /
93.\}
94.if t \{\
95. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u"
96. ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'
97. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'
98. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'
99. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'
100. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'
101.\}
102. \" troff and (daisy-wheel) nroff accents
103.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'
104.ds 8 \h'\*(#H'\(*b\h'-\*(#H'
105.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#]
106.ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'
107.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'
108.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#]
109.ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#]
110.ds ae a\h'-(\w'a'u*4/10)'e
111.ds Ae A\h'-(\w'A'u*4/10)'E
112. \" corrections for vroff
113.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'
114.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'
115. \" for low resolution devices (crt and lpr)
116.if \n(.H>23 .if \n(.V>19 \
117\{\
118. ds : e
119. ds 8 ss
120. ds o a
121. ds d- d\h'-1'\(ga
122. ds D- D\h'-1'\(hy
123. ds th \o'bp'
124. ds Th \o'LP'
125. ds ae ae
126. ds Ae AE
127.\}
128.rm #[ #] #H #V #F C
129.\" ========================================================================
130.\"
131.IX Title "PERLDEBTUT 1"
132.TH PERLDEBTUT 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide"
133.SH "NAME"
134perldebtut \- Perl debugging tutorial
135.SH "DESCRIPTION"
136.IX Header "DESCRIPTION"
137A (very) lightweight introduction in the use of the perl debugger, and a
138pointer to existing, deeper sources of information on the subject of debugging
139perl programs.
140.PP
141There's an extraordinary number of people out there who don't appear to know
142anything about using the perl debugger, though they use the language every
143day.
144This is for them.
145.SH "use strict"
146.IX Header "use strict"
147First of all, there's a few things you can do to make your life a lot more
148straightforward when it comes to debugging perl programs, without using the
149debugger at all. To demonstrate, here's a simple script, named \*(L"hello\*(R", with
150a problem:
151.PP
152.Vb 1
153\& #!/usr/bin/perl
154.Ve
155.PP
156.Vb 2
157\& $var1 = 'Hello World'; # always wanted to do that :-)
158\& $var2 = "$varl\en";
159.Ve
160.PP
161.Vb 2
162\& print $var2;
163\& exit;
164.Ve
165.PP
166While this compiles and runs happily, it probably won't do what's expected,
167namely it doesn't print \*(L"Hello World\en\*(R" at all; It will on the other hand do
168exactly what it was told to do, computers being a bit that way inclined. That
169is, it will print out a newline character, and you'll get what looks like a
170blank line. It looks like there's 2 variables when (because of the typo)
171there's really 3:
172.PP
173.Vb 3
174\& $var1 = 'Hello World';
175\& $varl = undef;
176\& $var2 = "\en";
177.Ve
178.PP
179To catch this kind of problem, we can force each variable to be declared
180before use by pulling in the strict module, by putting 'use strict;' after the
181first line of the script.
182.PP
183Now when you run it, perl complains about the 3 undeclared variables and we
184get four error messages because one variable is referenced twice:
185.PP
186.Vb 5
187\& Global symbol "$var1" requires explicit package name at ./t1 line 4.
188\& Global symbol "$var2" requires explicit package name at ./t1 line 5.
189\& Global symbol "$varl" requires explicit package name at ./t1 line 5.
190\& Global symbol "$var2" requires explicit package name at ./t1 line 7.
191\& Execution of ./hello aborted due to compilation errors.
192.Ve
193.PP
194Luvverly! and to fix this we declare all variables explicitly and now our
195script looks like this:
196.PP
197.Vb 2
198\& #!/usr/bin/perl
199\& use strict;
200.Ve
201.PP
202.Vb 3
203\& my $var1 = 'Hello World';
204\& my $varl = undef;
205\& my $var2 = "$varl\en";
206.Ve
207.PP
208.Vb 2
209\& print $var2;
210\& exit;
211.Ve
212.PP
213We then do (always a good idea) a syntax check before we try to run it again:
214.PP
215.Vb 2
216\& > perl -c hello
217\& hello syntax OK
218.Ve
219.PP
220And now when we run it, we get \*(L"\en\*(R" still, but at least we know why. Just
221getting this script to compile has exposed the '$varl' (with the letter 'l')
222variable, and simply changing \f(CW$varl\fR to \f(CW$var1\fR solves the problem.
223.SH "Looking at data and \-w and v"
224.IX Header "Looking at data and -w and v"
225Ok, but how about when you want to really see your data, what's in that
226dynamic variable, just before using it?
227.PP
228.Vb 2
229\& #!/usr/bin/perl
230\& use strict;
231.Ve
232.PP
233.Vb 8
234\& my $key = 'welcome';
235\& my %data = (
236\& 'this' => qw(that),
237\& 'tom' => qw(and jerry),
238\& 'welcome' => q(Hello World),
239\& 'zip' => q(welcome),
240\& );
241\& my @data = keys %data;
242.Ve
243.PP
244.Vb 2
245\& print "$data{$key}\en";
246\& exit;
247.Ve
248.PP
249Looks \s-1OK\s0, after it's been through the syntax check (perl \-c scriptname), we
250run it and all we get is a blank line again! Hmmmm.
251.PP
252One common debugging approach here, would be to liberally sprinkle a few print
253statements, to add a check just before we print out our data, and another just
254after:
255.PP
256.Vb 3
257\& print "All OK\en" if grep($key, keys %data);
258\& print "$data{$key}\en";
259\& print "done: '$data{$key}'\en";
260.Ve
261.PP
262And try again:
263.PP
264.Vb 2
265\& > perl data
266\& All OK
267.Ve
268.PP
269.Vb 1
270\& done: ''
271.Ve
272.PP
273After much staring at the same piece of code and not seeing the wood for the
274trees for some time, we get a cup of coffee and try another approach. That
275is, we bring in the cavalry by giving perl the '\fB\-d\fR' switch on the command
276line:
277.PP
278.Vb 2
279\& > perl -d data
280\& Default die handler restored.
281.Ve
282.PP
283.Vb 2
284\& Loading DB routines from perl5db.pl version 1.07
285\& Editor support available.
286.Ve
287.PP
288.Vb 1
289\& Enter h or `h h' for help, or `man perldebug' for more help.
290.Ve
291.PP
292.Vb 1
293\& main::(./data:4): my $key = 'welcome';
294.Ve
295.PP
296Now, what we've done here is to launch the built-in perl debugger on our
297script. It's stopped at the first line of executable code and is waiting for
298input.
299.PP
300Before we go any further, you'll want to know how to quit the debugger: use
301just the letter '\fBq\fR', not the words 'quit' or 'exit':
302.PP
303.Vb 2
304\& DB<1> q
305\& >
306.Ve
307.PP
308That's it, you're back on home turf again.
309.SH "help"
310.IX Header "help"
311Fire the debugger up again on your script and we'll look at the help menu.
312There's a couple of ways of calling help: a simple '\fBh\fR' will get the summary
313help list, '\fB|h\fR' (pipe\-h) will pipe the help through your pager (which is
314(probably 'more' or 'less'), and finally, '\fBh h\fR' (h\-space\-h) will give you
315the entire help screen. Here is the summary page:
316.PP
317D\fB1\fRh
318.PP
319.Vb 25
320\& List/search source lines: Control script execution:
321\& l [ln|sub] List source code T Stack trace
322\& - or . List previous/current line s [expr] Single step [in expr]
323\& v [line] View around line n [expr] Next, steps over subs
324\& f filename View source in file <CR/Enter> Repeat last n or s
325\& /pattern/ ?patt? Search forw/backw r Return from subroutine
326\& M Show module versions c [ln|sub] Continue until position
327\& Debugger controls: L List break/watch/actions
328\& o [...] Set debugger options t [expr] Toggle trace [trace expr]
329\& <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
330\& ! [N|pat] Redo a previous command B ln|* Delete a/all breakpoints
331\& H [-num] Display last num commands a [ln] cmd Do cmd before line
332\& = [a val] Define/list an alias A ln|* Delete a/all actions
333\& h [db_cmd] Get help on command w expr Add a watch expression
334\& h h Complete help page W expr|* Delete a/all watch exprs
335\& |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess
336\& q or ^D Quit R Attempt a restart
337\& Data Examination: expr Execute perl code, also see: s,n,t expr
338\& x|m expr Evals expr in list context, dumps the result or lists methods.
339\& p expr Print expression (uses script's current package).
340\& S [[!]pat] List subroutine names [not] matching pattern
341\& V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
342\& X [Vars] Same as "V current_package [Vars]".
343\& y [n [Vars]] List lexicals in higher scope <n>. Vars same as V.
344\& For more help, type h cmd_letter, or run man perldebug for all docs.
345.Ve
346.PP
347More confusing options than you can shake a big stick at! It's not as bad as
348it looks and it's very useful to know more about all of it, and fun too!
349.PP
350There's a couple of useful ones to know about straight away. You wouldn't
351think we're using any libraries at all at the moment, but '\fBM\fR' will show
352which modules are currently loaded, and their version number, while '\fBm\fR'
353will show the methods, and '\fBS\fR' shows all subroutines (by pattern) as
354shown below. '\fBV\fR' and '\fBX\fR' show variables in the program by package
355scope and can be constrained by pattern.
356.PP
357.Vb 5
358\& DB<2>S str
359\& dumpvar::stringify
360\& strict::bits
361\& strict::import
362\& strict::unimport
363.Ve
364.PP
365Using 'X' and cousins requires you not to use the type identifiers ($@%), just
366the 'name':
367.PP
368.Vb 2
369\& DM<3>X ~err
370\& FileHandle(stderr) => fileno(2)
371.Ve
372.PP
373Remember we're in our tiny program with a problem, we should have a look at
374where we are, and what our data looks like. First of all let's view some code
375at our present position (the first line of code in this case), via '\fBv\fR':
376.PP
377.Vb 11
378\& DB<4> v
379\& 1 #!/usr/bin/perl
380\& 2: use strict;
381\& 3
382\& 4==> my $key = 'welcome';
383\& 5: my %data = (
384\& 6 'this' => qw(that),
385\& 7 'tom' => qw(and jerry),
386\& 8 'welcome' => q(Hello World),
387\& 9 'zip' => q(welcome),
388\& 10 );
389.Ve
390.PP
391At line number 4 is a helpful pointer, that tells you where you are now. To
392see more code, type 'v' again:
393.PP
394.Vb 9
395\& DB<4> v
396\& 8 'welcome' => q(Hello World),
397\& 9 'zip' => q(welcome),
398\& 10 );
399\& 11: my @data = keys %data;
400\& 12: print "All OK\en" if grep($key, keys %data);
401\& 13: print "$data{$key}\en";
402\& 14: print "done: '$data{$key}'\en";
403\& 15: exit;
404.Ve
405.PP
406And if you wanted to list line 5 again, type 'l 5', (note the space):
407.PP
408.Vb 2
409\& DB<4> l 5
410\& 5: my %data = (
411.Ve
412.PP
413In this case, there's not much to see, but of course normally there's pages of
414stuff to wade through, and 'l' can be very useful. To reset your view to the
415line we're about to execute, type a lone period '.':
416.PP
417.Vb 2
418\& DB<5> .
419\& main::(./data_a:4): my $key = 'welcome';
420.Ve
421.PP
422The line shown is the one that is about to be executed \fBnext\fR, it hasn't
423happened yet. So while we can print a variable with the letter '\fBp\fR', at
424this point all we'd get is an empty (undefined) value back. What we need to
425do is to step through the next executable statement with an '\fBs\fR':
426.PP
427.Vb 7
428\& DB<6> s
429\& main::(./data_a:5): my %data = (
430\& main::(./data_a:6): 'this' => qw(that),
431\& main::(./data_a:7): 'tom' => qw(and jerry),
432\& main::(./data_a:8): 'welcome' => q(Hello World),
433\& main::(./data_a:9): 'zip' => q(welcome),
434\& main::(./data_a:10): );
435.Ve
436.PP
437Now we can have a look at that first ($key) variable:
438.PP
439.Vb 2
440\& DB<7> p $key
441\& welcome
442.Ve
443.PP
444line 13 is where the action is, so let's continue down to there via the letter
445\&'\fBc\fR', which by the way, inserts a 'one\-time\-only' breakpoint at the given
446line or sub routine:
447.PP
448.Vb 3
449\& DB<8> c 13
450\& All OK
451\& main::(./data_a:13): print "$data{$key}\en";
452.Ve
453.PP
454We've gone past our check (where 'All \s-1OK\s0' was printed) and have stopped just
455before the meat of our task. We could try to print out a couple of variables
456to see what is happening:
457.PP
458.Vb 1
459\& DB<9> p $data{$key}
460.Ve
461.PP
462Not much in there, lets have a look at our hash:
463.PP
464.Vb 2
465\& DB<10> p %data
466\& Hello Worldziptomandwelcomejerrywelcomethisthat
467.Ve
468.PP
469.Vb 2
470\& DB<11> p keys %data
471\& Hello Worldtomwelcomejerrythis
472.Ve
473.PP
474Well, this isn't very easy to read, and using the helpful manual (\fBh h\fR), the
475\&'\fBx\fR' command looks promising:
476.PP
477.Vb 11
478\& DB<12> x %data
479\& 0 'Hello World'
480\& 1 'zip'
481\& 2 'tom'
482\& 3 'and'
483\& 4 'welcome'
484\& 5 undef
485\& 6 'jerry'
486\& 7 'welcome'
487\& 8 'this'
488\& 9 'that'
489.Ve
490.PP
491That's not much help, a couple of welcomes in there, but no indication of
492which are keys, and which are values, it's just a listed array dump and, in
493this case, not particularly helpful. The trick here, is to use a \fBreference\fR
494to the data structure:
495.PP
496.Vb 7
497\& DB<13> x \e%data
498\& 0 HASH(0x8194bc4)
499\& 'Hello World' => 'zip'
500\& 'jerry' => 'welcome'
501\& 'this' => 'that'
502\& 'tom' => 'and'
503\& 'welcome' => undef
504.Ve
505.PP
506The reference is truly dumped and we can finally see what we're dealing with.
507Our quoting was perfectly valid but wrong for our purposes, with 'and jerry'
508being treated as 2 separate words rather than a phrase, thus throwing the
509evenly paired hash structure out of alignment.
510.PP
511The '\fB\-w\fR' switch would have told us about this, had we used it at the start,
512and saved us a lot of trouble:
513.PP
514.Vb 2
515\& > perl -w data
516\& Odd number of elements in hash assignment at ./data line 5.
517.Ve
518.PP
519We fix our quoting: 'tom' => q(and jerry), and run it again, this time we get
520our expected output:
521.PP
522.Vb 2
523\& > perl -w data
524\& Hello World
525.Ve
526.PP
527While we're here, take a closer look at the '\fBx\fR' command, it's really useful
528and will merrily dump out nested references, complete objects, partial objects
529\&\- just about whatever you throw at it:
530.PP
531Let's make a quick object and x\-plode it, first we'll start the debugger:
532it wants some form of input from \s-1STDIN\s0, so we give it something non\-committal,
533a zero:
534.PP
535.Vb 2
536\& > perl -de 0
537\& Default die handler restored.
538.Ve
539.PP
540.Vb 2
541\& Loading DB routines from perl5db.pl version 1.07
542\& Editor support available.
543.Ve
544.PP
545.Vb 1
546\& Enter h or `h h' for help, or `man perldebug' for more help.
547.Ve
548.PP
549.Vb 1
550\& main::(-e:1): 0
551.Ve
552.PP
553Now build an on-the-fly object over a couple of lines (note the backslash):
554.PP
555.Vb 2
556\& DB<1> $obj = bless({'unique_id'=>'123', 'attr'=> \e
557\& cont: {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
558.Ve
559.PP
560And let's have a look at it:
561.PP
562.Vb 10
563\& DB<2> x $obj
564\& 0 MY_class=HASH(0x828ad98)
565\& 'attr' => HASH(0x828ad68)
566\& 'col' => 'black'
567\& 'things' => ARRAY(0x828abb8)
568\& 0 'this'
569\& 1 'that'
570\& 2 'etc'
571\& 'unique_id' => 123
572\& DB<3>
573.Ve
574.PP
575Useful, huh? You can eval nearly anything in there, and experiment with bits
576of code or regexes until the cows come home:
577.PP
578.Vb 1
579\& DB<3> @data = qw(this that the other atheism leather theory scythe)
580.Ve
581.PP
582.Vb 8
583\& DB<4> p 'saw -> '.($cnt += map { print "\et:\et$_\en" } grep(/the/, sort @data))
584\& atheism
585\& leather
586\& other
587\& scythe
588\& the
589\& theory
590\& saw -> 6
591.Ve
592.PP
593If you want to see the command History, type an '\fBH\fR':
594.PP
595.Vb 7
596\& DB<5> H
597\& 4: p 'saw -> '.($cnt += map { print "\et:\et$_\en" } grep(/the/, sort @data))
598\& 3: @data = qw(this that the other atheism leather theory scythe)
599\& 2: x $obj
600\& 1: $obj = bless({'unique_id'=>'123', 'attr'=>
601\& {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
602\& DB<5>
603.Ve
604.PP
605And if you want to repeat any previous command, use the exclamation: '\fB!\fR':
606.PP
607.Vb 9
608\& DB<5> !4
609\& p 'saw -> '.($cnt += map { print "$_\en" } grep(/the/, sort @data))
610\& atheism
611\& leather
612\& other
613\& scythe
614\& the
615\& theory
616\& saw -> 12
617.Ve
618.PP
619For more on references see perlref and perlreftut
620.SH "Stepping through code"
621.IX Header "Stepping through code"
622Here's a simple program which converts between Celsius and Fahrenheit, it too
623has a problem:
624.PP
625.Vb 2
626\& #!/usr/bin/perl -w
627\& use strict;
628.Ve
629.PP
630.Vb 1
631\& my $arg = $ARGV[0] || '-c20';
632.Ve
633.PP
634.Vb 17
635\& if ($arg =~ /^\e-(c|f)((\e-|\e+)*\ed+(\e.\ed+)*)$/) {
636\& my ($deg, $num) = ($1, $2);
637\& my ($in, $out) = ($num, $num);
638\& if ($deg eq 'c') {
639\& $deg = 'f';
640\& $out = &c2f($num);
641\& } else {
642\& $deg = 'c';
643\& $out = &f2c($num);
644\& }
645\& $out = sprintf('%0.2f', $out);
646\& $out =~ s/^((\e-|\e+)*\ed+)\e.0+$/$1/;
647\& print "$out $deg\en";
648\& } else {
649\& print "Usage: $0 -[c|f] num\en";
650\& }
651\& exit;
652.Ve
653.PP
654.Vb 5
655\& sub f2c {
656\& my $f = shift;
657\& my $c = 5 * $f - 32 / 9;
658\& return $c;
659\& }
660.Ve
661.PP
662.Vb 5
663\& sub c2f {
664\& my $c = shift;
665\& my $f = 9 * $c / 5 + 32;
666\& return $f;
667\& }
668.Ve
669.PP
670For some reason, the Fahrenheit to Celsius conversion fails to return the
671expected output. This is what it does:
672.PP
673.Vb 2
674\& > temp -c0.72
675\& 33.30 f
676.Ve
677.PP
678.Vb 2
679\& > temp -f33.3
680\& 162.94 c
681.Ve
682.PP
683Not very consistent! We'll set a breakpoint in the code manually and run it
684under the debugger to see what's going on. A breakpoint is a flag, to which
685the debugger will run without interruption, when it reaches the breakpoint, it
686will stop execution and offer a prompt for further interaction. In normal
687use, these debugger commands are completely ignored, and they are safe \- if a
688little messy, to leave in production code.
689.PP
690.Vb 4
691\& my ($in, $out) = ($num, $num);
692\& $DB::single=2; # insert at line 9!
693\& if ($deg eq 'c')
694\& ...
695.Ve
696.PP
697.Vb 2
698\& > perl -d temp -f33.3
699\& Default die handler restored.
700.Ve
701.PP
702.Vb 2
703\& Loading DB routines from perl5db.pl version 1.07
704\& Editor support available.
705.Ve
706.PP
707.Vb 1
708\& Enter h or `h h' for help, or `man perldebug' for more help.
709.Ve
710.PP
711.Vb 1
712\& main::(temp:4): my $arg = $ARGV[0] || '-c100';
713.Ve
714.PP
715We'll simply continue down to our pre-set breakpoint with a '\fBc\fR':
716.PP
717.Vb 2
718\& DB<1> c
719\& main::(temp:10): if ($deg eq 'c') {
720.Ve
721.PP
722Followed by a view command to see where we are:
723.PP
724.Vb 11
725\& DB<1> v
726\& 7: my ($deg, $num) = ($1, $2);
727\& 8: my ($in, $out) = ($num, $num);
728\& 9: $DB::single=2;
729\& 10==> if ($deg eq 'c') {
730\& 11: $deg = 'f';
731\& 12: $out = &c2f($num);
732\& 13 } else {
733\& 14: $deg = 'c';
734\& 15: $out = &f2c($num);
735\& 16 }
736.Ve
737.PP
738And a print to show what values we're currently using:
739.PP
740.Vb 2
741\& DB<1> p $deg, $num
742\& f33.3
743.Ve
744.PP
745We can put another break point on any line beginning with a colon, we'll use
746line 17 as that's just as we come out of the subroutine, and we'd like to
747pause there later on:
748.PP
749.Vb 1
750\& DB<2> b 17
751.Ve
752.PP
753There's no feedback from this, but you can see what breakpoints are set by
754using the list 'L' command:
755.PP
756.Vb 4
757\& DB<3> L
758\& temp:
759\& 17: print "$out $deg\en";
760\& break if (1)
761.Ve
762.PP
763Note that to delete a breakpoint you use 'd' or 'D'.
764.PP
765Now we'll continue down into our subroutine, this time rather than by line
766number, we'll use the subroutine name, followed by the now familiar 'v':
767.PP
768.Vb 2
769\& DB<3> c f2c
770\& main::f2c(temp:30): my $f = shift;
771.Ve
772.PP
773.Vb 11
774\& DB<4> v
775\& 24: exit;
776\& 25
777\& 26 sub f2c {
778\& 27==> my $f = shift;
779\& 28: my $c = 5 * $f - 32 / 9;
780\& 29: return $c;
781\& 30 }
782\& 31
783\& 32 sub c2f {
784\& 33: my $c = shift;
785.Ve
786.PP
787Note that if there was a subroutine call between us and line 29, and we wanted
788to \fBsingle-step\fR through it, we could use the '\fBs\fR' command, and to step
789over it we would use '\fBn\fR' which would execute the sub, but not descend into
790it for inspection. In this case though, we simply continue down to line 29:
791.PP
792.Vb 2
793\& DB<4> c 29
794\& main::f2c(temp:29): return $c;
795.Ve
796.PP
797And have a look at the return value:
798.PP
799.Vb 2
800\& DB<5> p $c
801\& 162.944444444444
802.Ve
803.PP
804This is not the right answer at all, but the sum looks correct. I wonder if
805it's anything to do with operator precedence? We'll try a couple of other
806possibilities with our sum:
807.PP
808.Vb 2
809\& DB<6> p (5 * $f - 32 / 9)
810\& 162.944444444444
811.Ve
812.PP
813.Vb 2
814\& DB<7> p 5 * $f - (32 / 9)
815\& 162.944444444444
816.Ve
817.PP
818.Vb 2
819\& DB<8> p (5 * $f) - 32 / 9
820\& 162.944444444444
821.Ve
822.PP
823.Vb 2
824\& DB<9> p 5 * ($f - 32) / 9
825\& 0.722222222222221
826.Ve
827.PP
828:\-) that's more like it! Ok, now we can set our return variable and we'll
829return out of the sub with an 'r':
830.PP
831.Vb 1
832\& DB<10> $c = 5 * ($f - 32) / 9
833.Ve
834.PP
835.Vb 2
836\& DB<11> r
837\& scalar context return from main::f2c: 0.722222222222221
838.Ve
839.PP
840Looks good, let's just continue off the end of the script:
841.PP
842.Vb 5
843\& DB<12> c
844\& 0.72 c
845\& Debugged program terminated. Use q to quit or R to restart,
846\& use O inhibit_exit to avoid stopping after program termination,
847\& h q, h R or h O to get additional info.
848.Ve
849.PP
850A quick fix to the offending line (insert the missing parentheses) in the
851actual program and we're finished.
852.SH "Placeholder for a, w, t, T"
853.IX Header "Placeholder for a, w, t, T"
854Actions, watch variables, stack traces etc.: on the \s-1TODO\s0 list.
855.PP
856.Vb 1
857\& a
858.Ve
859.PP
860.Vb 1
861\& w
862.Ve
863.PP
864.Vb 1
865\& t
866.Ve
867.PP
868.Vb 1
869\& T
870.Ve
871.SH "REGULAR EXPRESSIONS"
872.IX Header "REGULAR EXPRESSIONS"
873Ever wanted to know what a regex looked like? You'll need perl compiled with
874the \s-1DEBUGGING\s0 flag for this one:
875.PP
876.Vb 18
877\& > perl -Dr -e '/^pe(a)*rl$/i'
878\& Compiling REx `^pe(a)*rl$'
879\& size 17 first at 2
880\& rarest char
881\& at 0
882\& 1: BOL(2)
883\& 2: EXACTF <pe>(4)
884\& 4: CURLYN[1] {0,32767}(14)
885\& 6: NOTHING(8)
886\& 8: EXACTF <a>(0)
887\& 12: WHILEM(0)
888\& 13: NOTHING(14)
889\& 14: EXACTF <rl>(16)
890\& 16: EOL(17)
891\& 17: END(0)
892\& floating `'$ at 4..2147483647 (checking floating) stclass `EXACTF <pe>'
893\&anchored(BOL) minlen 4
894\& Omitting $` $& $' support.
895.Ve
896.PP
897.Vb 1
898\& EXECUTING...
899.Ve
900.PP
901.Vb 1
902\& Freeing REx: `^pe(a)*rl$'
903.Ve
904.PP
905Did you really want to know? :\-)
906For more gory details on getting regular expressions to work, have a look at
907perlre, perlretut, and to decode the mysterious labels (\s-1BOL\s0 and \s-1CURLYN\s0,
908etc. above), see perldebguts.
909.SH "OUTPUT TIPS"
910.IX Header "OUTPUT TIPS"
911To get all the output from your error log, and not miss any messages via
912helpful operating system buffering, insert a line like this, at the start of
913your script:
914.PP
915.Vb 1
916\& $|=1;
917.Ve
918.PP
919To watch the tail of a dynamically growing logfile, (from the command line):
920.PP
921.Vb 1
922\& tail -f $error_log
923.Ve
924.PP
925Wrapping all die calls in a handler routine can be useful to see how, and from
926where, they're being called, perlvar has more information:
927.PP
928.Vb 1
929\& BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } }
930.Ve
931.PP
932Various useful techniques for the redirection of \s-1STDOUT\s0 and \s-1STDERR\s0 filehandles
933are explained in perlopentut and perlfaq8.
934.SH "CGI"
935.IX Header "CGI"
936Just a quick hint here for all those \s-1CGI\s0 programmers who can't figure out how
937on earth to get past that 'waiting for input' prompt, when running their \s-1CGI\s0
938script from the command\-line, try something like this:
939.PP
940.Vb 1
941\& > perl -d my_cgi.pl -nodebug
942.Ve
943.PP
944Of course \s-1CGI\s0 and perlfaq9 will tell you more.
945.SH "GUIs"
946.IX Header "GUIs"
947The command line interface is tightly integrated with an \fBemacs\fR extension
948and there's a \fBvi\fR interface too.
949.PP
950You don't have to do this all on the command line, though, there are a few \s-1GUI\s0
951options out there. The nice thing about these is you can wave a mouse over a
952variable and a dump of its data will appear in an appropriate window, or in a
953popup balloon, no more tiresome typing of 'x \f(CW$varname\fR' :\-)
954.PP
955In particular have a hunt around for the following:
956.PP
957\&\fBptkdb\fR perlTK based wrapper for the built-in debugger
958.PP
959\&\fBddd\fR data display debugger
960.PP
961\&\fBPerlDevKit\fR and \fBPerlBuilder\fR are \s-1NT\s0 specific
962.PP
963\&\s-1NB\s0. (more info on these and others would be appreciated).
964.SH "SUMMARY"
965.IX Header "SUMMARY"
966We've seen how to encourage good coding practices with \fBuse strict\fR and
967\&\fB\-w\fR. We can run the perl debugger \fBperl \-d scriptname\fR to inspect your
968data from within the perl debugger with the \fBp\fR and \fBx\fR commands. You can
969walk through your code, set breakpoints with \fBb\fR and step through that code
970with \fBs\fR or \fBn\fR, continue with \fBc\fR and return from a sub with \fBr\fR. Fairly
971intuitive stuff when you get down to it.
972.PP
973There is of course lots more to find out about, this has just scratched the
974surface. The best way to learn more is to use perldoc to find out more about
975the language, to read the on-line help (perldebug is probably the next
976place to go), and of course, experiment.
977.SH "SEE ALSO"
978.IX Header "SEE ALSO"
979perldebug,
980perldebguts,
981perldiag,
982dprofpp,
983perlrun
984.SH "AUTHOR"
985.IX Header "AUTHOR"
986Richard Foley <richard@rfi.net> Copyright (c) 2000
987.SH "CONTRIBUTORS"
988.IX Header "CONTRIBUTORS"
989Various people have made helpful suggestions and contributions, in particular:
990.PP
991Ronald J Kimball <rjk@linguist.dartmouth.edu>
992.PP
993Hugo van der Sanden <hv@crypt0.demon.co.uk>
994.PP
995Peter Scott <Peter@PSDT.com>