File: perldebtut.html

package info (click to toggle)
perl-doc-html 5.26.0-4
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 39,400 kB
  • sloc: xml: 36; makefile: 7
file content (684 lines) | stat: -rw-r--r-- 63,702 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
  <title>perldebtut - perldoc.perl.org</title>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <meta http-equiv="Content-Language" content="en-gb">
  <link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
  <link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
  <link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>

<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
    <div id="page">
      
      <div id="header">
	<div id="homepage_link">
	  <a href="index.html"></a>
	</div>
	<div id="strapline">
	  Perl Programming Documentation
	</div>
	<div id="download_link" class="download">
	  <a href="http://www.perl.org/get.html">Download Perl</a>
	</div>
	<div id="explore_link" class="download">
	  <a id="explore_anchor" href="#">Explore</a>
	</div>
      </div>
      
      <div id="body">
        <div id="left_column">
          <div class="side_group">
            
	    <div class="side_panel doc_panel">
              <p>Manual</p>
              <ul>
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">History / Changes</a>
                <li><a href="index-licence.html">License</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Reference</p>
              <ul>
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Special Variables</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platform Specific</a>
              </ul>
            </div>
            <div class="side_panel doc_panel">
              <p>Modules</p>
              <ul>
		<li>
		
                
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		    
		  
		
                  
		
                  
		
                  
		    
		  
		
                  
		
                  
		
		
                    <a href="index-modules-A.html">A</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-B.html">B</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-C.html">C</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-D.html">D</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-E.html">E</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-F.html">F</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-G.html">G</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-H.html">H</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-I.html">I</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-L.html">L</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-M.html">M</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-N.html">N</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-O.html">O</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-P.html">P</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-S.html">S</a>
                    
                      
                        <li>
                      
                    
                
                    <a href="index-modules-T.html">T</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-U.html">U</a>
                    
                      
                        &bull;
                      
                    
                
                    <a href="index-modules-X.html">X</a>
                    
                
              </ul>
            </div>
            
	      <div class="side_panel doc_panel">
		<p>Tools</p>
		<ul>
		  <li><a href="preferences.html">Preferences</a>
		</ul>
	      </div>
            
          </div>
        </div>
        <div id="centre_column">
          <div id="content_header">
            <div id="title_bar">
              <div id="page_name">
                <h1>perldebtut</h1>
              </div>
              <div id="perl_version">
                Perl 5 version 26.0 documentation
              </div>
              <div class="page_links" id="page_links_top">
                <a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
		
              </div>
	      <div class="page_links" id="page_links_bottom">
		
                  <a href="#" id="page_index_toggle">Show page index</a> &bull;
		
                <a href="#" id="recent_pages_toggle">Show recent pages</a>		
	      </div>
	      <div id="search_form">
		<form action="search.html" method="GET" id="search">
		  <input type="text" name="q" id="search_box" alt="Search">
		</form>
	      </div>
            </div>
            <div id="breadcrumbs">
                
    <a href="index.html">Home</a> &gt;
    
      
        <a href="index-tutorials.html">Tutorials</a> &gt;
      
    
    perldebtut
  

            </div>
          </div>
          <div id="content_body">
	    <!--[if lt IE 7]>
 <div class="noscript">
   <p>
     <strong>It looks like you're using Internet Explorer 6. This is a very old
     browser which does not offer full support for modern websites.</strong>
   </p>
   <p>
     Unfortunately this means that this website will not work on
     your computer.
   </p>
   <p>
     Don't miss out though! To view the site (and get a better experience from
     many other websites), simply upgrade to
     <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
     or download an alternative browser such as
     <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
     <a href="http://www.apple.com/safari/download/">Safari</a>, or
     <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
   </p>
   <p>
     All of these browsers are free. If you're using a PC at work, you may
     need to contact your IT administrator.
   </p>
 </div>
<![endif]-->
	    <noscript>
	      <div class="noscript">
	      <p>
                <strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
	        or are running a non-JavaScript capable web browser.</strong>
	      </p>
	      <p>
		To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
              </p>
	      </div>
	    </noscript>

	    <div id="recent_pages" class="hud_container">
	      <div id="recent_pages_header" class="hud_header">
		<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
		<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
		<div id="recent_pages_topright" class="hud_topright"></div>
	      </div>
	      <div id="recent_pages_content" class="hud_content">
	      </div>
	      <div id="recent_pages_footer" class="hud_footer">
		<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
		<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
		<div id="recent_pages_resize" class="hud_resize"></div>
	      </div>
	    </div>
  
	    <div id="from_search"></div>
            <h1>perldebtut</h1>


  <!--    -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#use-strict">use strict</a><li><a href="#Looking-at-data-and--w-and-v">Looking at data and -w and v</a><li><a href="#help">help</a><li><a href="#Stepping-through-code">Stepping through code</a><li><a href="#Placeholder-for-a%2c-w%2c-t%2c-T">Placeholder for a, w, t, T</a><li><a href="#REGULAR-EXPRESSIONS">REGULAR EXPRESSIONS</a><li><a href="#OUTPUT-TIPS">OUTPUT TIPS</a><li><a href="#CGI">CGI</a><li><a href="#GUIs">GUIs</a><li><a href="#SUMMARY">SUMMARY</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#CONTRIBUTORS">CONTRIBUTORS</a></ul><a name="NAME"></a><h1>NAME</h1>
<p>perldebtut - Perl debugging tutorial</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>A (very) lightweight introduction in the use of the perl debugger, and a
pointer to existing, deeper sources of information on the subject of debugging
perl programs.</p>
<p>There's an extraordinary number of people out there who don't appear to know
anything about using the perl debugger, though they use the language every
day.  
This is for them.</p>
<a name="use-strict"></a><h1>use strict</h1>
<p>First of all, there's a few things you can do to make your life a lot more
straightforward when it comes to debugging perl programs, without using the
debugger at all.  To demonstrate, here's a simple script, named "hello", with
a problem:</p>
<pre class="verbatim"><ol><li>	<span class="c">#!/usr/bin/perl</span></li><li></li><li>	<span class="i">$var1</span> = <span class="q">&#39;Hello World&#39;</span><span class="sc">;</span> <span class="c"># always wanted to do that :-)</span></li><li>	<span class="i">$var2</span> = <span class="q">&quot;$varl\n&quot;</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="i">$var2</span><span class="sc">;</span> </li><li>	<a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></li></ol></pre><p>While this compiles and runs happily, it probably won't do what's expected,
namely it doesn't print "Hello World\n" 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:</p>
<pre class="verbatim"><ol><li>	<span class="i">$var1</span> = <span class="q">&#39;Hello World&#39;</span><span class="sc">;</span></li><li>	<span class="i">$varl</span> = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>	<span class="i">$var2</span> = <span class="q">&quot;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>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.</p>
<p>Now when you run it, perl complains about the 3 undeclared variables and we
get four error messages because one variable is referenced twice:</p>
<pre class="verbatim"><ol><li> Global symbol "$var1" requires explicit package name at ./t1 line 4.</li><li> Global symbol "$var2" requires explicit package name at ./t1 line 5.</li><li> Global symbol "$varl" requires explicit package name at ./t1 line 5.</li><li> Global symbol "$var2" requires explicit package name at ./t1 line 7.</li><li> Execution of ./hello aborted due to compilation errors.</li></ol></pre><p>Luvverly! and to fix this we declare all variables explicitly and now our
script looks like this:</p>
<pre class="verbatim"><ol><li>	<span class="c">#!/usr/bin/perl</span></li><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$var1</span> = <span class="q">&#39;Hello World&#39;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$varl</span> = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$var2</span> = <span class="q">&quot;$varl\n&quot;</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="i">$var2</span><span class="sc">;</span> </li><li>	<a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></li></ol></pre><p>We then do (always a good idea) a syntax check before we try to run it again:</p>
<pre class="verbatim"><ol><li>	&gt; perl -c hello</li><li>	hello syntax OK</li></ol></pre><p>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.</p>
<a name="Looking-at-data-and--w-and-v"></a><h1>Looking at data and -w and v</h1>
<p>Ok, but how about when you want to really see your data, what's in that
dynamic variable, just before using it?</p>
<pre class="verbatim"><ol><li>	<span class="c">#!/usr/bin/perl </span></li><li>	<a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$key</span> = <span class="q">&#39;welcome&#39;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">%data</span> = <span class="s">(</span></li><li>		<span class="q">&#39;this&#39;</span> <span class="cm">=&gt;</span> <span class="q">qw(that)</span><span class="cm">,</span> </li><li>		<span class="q">&#39;tom&#39;</span> <span class="cm">=&gt;</span> <span class="q">qw(and jerry)</span><span class="cm">,</span></li><li>		<span class="q">&#39;welcome&#39;</span> <span class="cm">=&gt;</span> <span class="q">q(Hello World)</span><span class="cm">,</span></li><li>		<span class="q">&#39;zip&#39;</span> <span class="cm">=&gt;</span> <span class="q">q(welcome)</span><span class="cm">,</span></li><li>	<span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">@data</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%data</span><span class="sc">;</span></li><li></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$data{$key}\n&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></li></ol></pre><p>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.</p>
<p>One common debugging approach here, would be to liberally sprinkle a few print
statements, to add a check just before we print out our data, and another just
after:</p>
<pre class="verbatim"><ol><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;All OK\n&quot;</span> if <a class="l_k" href="functions/grep.html">grep</a><span class="s">(</span><span class="i">$key</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%data</span><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$data{$key}\n&quot;</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;done: &#39;$data{$key}&#39;\n&quot;</span><span class="sc">;</span></li></ol></pre><p>And try again:</p>
<pre class="verbatim"><ol><li>	&gt; perl data</li><li>	All OK     </li><li></li><li>	done: ''</li></ol></pre><p>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 '<b>-d</b>' switch on the command
line:</p>
<pre class="verbatim"><ol><li>	&gt; perl -d data </li><li>	Default die handler restored.</li><li></li><li>	Loading DB routines from perl5db.pl version 1.07</li><li>	Editor support available.</li><li></li><li>	Enter h or `h h' for help, or `man perldebug' for more help.</li><li></li><li>	main::(./data:4):     my $key = 'welcome';</li></ol></pre><p>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.</p>
<p>Before we go any further, you'll want to know how to quit the debugger: use
just the letter '<b>q</b>', not the words 'quit' or 'exit':</p>
<pre class="verbatim"><ol><li>	DB&lt;1&gt; q</li><li>	&gt;</li></ol></pre><p>That's it, you're back on home turf again.</p>
<a name="help"></a><h1>help</h1>
<p>Fire the debugger up again on your script and we'll look at the help menu. 
There's a couple of ways of calling help: a simple '<b>h</b>' will get the summary 
help list, '<b>|h</b>' (pipe-h) will pipe the help through your pager (which is 
(probably 'more' or 'less'), and finally, '<b>h h</b>' (h-space-h) will give you 
the entire help screen.  Here is the summary page:</p>
<p>D<b>1</b>h</p>
<pre class="verbatim"><ol><li> <span class="w">List</span>/<span class="w">search</span> <span class="w">source</span> <span class="w">lines</span><span class="co">:</span>               <span class="w">Control</span> <span class="w">script</span> <span class="w">execution</span><span class="co">:</span></li><li>  <span class="w">l</span> <span class="s">[</span><span class="w">ln</span>|<a class="l_k" href="functions/sub.html">sub</a><span class="s">]</span>  <span class="w">List</span> <span class="w">source</span> <span class="w">code</span>            <span class="w">T</span>           <span class="w">Stack</span> <span class="w">trace</span></li><li>  - <a class="l_k" href="functions/or.html">or</a> .      <span class="w">List</span> <span class="w">previous</span>/<span class="w">current</span> <span class="w">line</span>  <span class="q">s [expr]    Single step</span></li><li>                                                               <span class="q">                                                               [in expr]</span></li><li>  <span class="q">  v [line]    View around line            n [expr]    Next, steps over</span></li><li>                                                                    <span class="q">                                                                    subs</span></li><li>  <span class="q">  f filename  View source in file         &lt;CR/Enter&gt;  Repeat last n or s</span></li><li>  <span class="q">  /pattern/ ?patt?   Search</span> <span class="w">forw</span>/<span class="w">backw</span>    <span class="w">r</span>           <span class="w">Return</span> <span class="w">from</span></li><li>                                                              <span class="w">subroutine</span></li><li>  <span class="w">M</span>           <span class="w">Show</span> <span class="w">module</span> <span class="w">versions</span>        <span class="w">c</span> <span class="s">[</span><span class="w">ln</span>|<a class="l_k" href="functions/sub.html">sub</a><span class="s">]</span>  <span class="w">Continue</span> <a class="l_k" href="functions/until.html">until</a></li><li>                                                                <span class="w">position</span></li><li> <span class="w">Debugger</span> <span class="w">controls</span><span class="co">:</span>                       <span class="w">L</span>           <span class="w">List</span> <a class="l_k" href="functions/break.html">break</a><span class="q">/watch/</span></li><li>                                                                 <span class="w">actions</span></li><li>  <span class="w">o</span> <span class="s">[</span>...<span class="s">]</span>     <span class="w">Set</span> <span class="w">debugger</span> <span class="w">options</span>        <span class="w">t</span> <span class="s">[</span><span class="w">expr</span><span class="s">]</span>    <span class="w">Toggle</span> <span class="w">trace</span></li><li>                                                            <span class="s">[</span><span class="w">trace</span> <span class="w">expr</span><span class="s">]</span></li><li>  <span class="q">&lt;[&lt;]|{[{]|&gt;</span><span class="s">[</span>&gt;<span class="s">]</span> <span class="s">[</span><span class="w">cmd</span><span class="s">]</span> <span class="w">Do</span> <span class="w">pre</span>/<span class="w">post</span>-<span class="w">prompt</span> <span class="w">b</span> <span class="s">[</span><span class="w">ln</span>|<span class="w">event</span>|<a class="l_k" href="functions/sub.html">sub</a><span class="s">]</span> <span class="s">[</span><span class="w">cnd</span><span class="s">]</span> <span class="w">Set</span></li><li>                                                              <span class="w">breakpoint</span></li><li>  ! <span class="s">[</span><span class="w">N</span>|<span class="w">pat</span><span class="s">]</span>   <span class="w">Redo</span> <span class="w">a</span> <span class="w">previous</span> <span class="w">command</span>     <span class="w">B</span> <span class="w">ln</span>|<span class="i">*      Delete</span> <span class="w">a</span>/<span class="w">all</span></li><li>                                                             <span class="w">breakpoints</span></li><li>  <span class="w">H</span> <span class="s">[</span>-<span class="w">num</span><span class="s">]</span>    <span class="w">Display</span> <a class="l_k" href="functions/last.html">last</a> <span class="j">num</span> <span class="w">commands</span>   <span class="w">a</span> <span class="s">[</span><span class="w">ln</span><span class="s">]</span> <span class="w">cmd</span>  <span class="w">Do</span> <span class="w">cmd</span> <span class="w">before</span> <span class="w">line</span></li><li>  = <span class="s">[</span><span class="w">a</span> <span class="w">val</span><span class="s">]</span>   <span class="w">Define</span>/<span class="w">list</span> <span class="w">an</span> <span class="w">alias</span>        <span class="w">A</span> <span class="w">ln</span>|<span class="i">*      Delete</span> <span class="w">a</span>/<span class="w">all</span></li><li>                                                                 <span class="w">actions</span></li><li>  <span class="w">h</span> <span class="s">[</span><span class="w">db_cmd</span><span class="s">]</span>  <span class="w">Get</span> <span class="w">help</span> <span class="w">on</span> <span class="w">command</span>         <span class="w">w</span> <span class="w">expr</span>      <span class="w">Add</span> <span class="w">a</span> <span class="w">watch</span></li><li>                                                              <span class="w">expression</span></li><li>  <span class="w">h</span> <span class="w">h</span>         <span class="w">Complete</span> <span class="w">help</span> <span class="w">page</span>          <span class="w">W</span> <span class="w">expr</span>|<span class="i">*    Delete</span> <span class="w">a</span>/<span class="w">all</span> <span class="w">watch</span></li><li>                                                                   <span class="w">exprs</span></li><li>  |<span class="s">[</span>|<span class="s">]</span><span class="w">db_cmd</span>  <span class="w">Send</span> <span class="w">output</span> <span class="w">to</span> <span class="w">pager</span>        !<span class="s">[</span>!<span class="s">]</span> <span class="w">syscmd</span> <span class="w">Run</span> <span class="w">cmd</span> <span class="w">in</span> <span class="w">a</span></li><li>                                                              <span class="w">subprocess</span></li><li>  <span class="q">q or ^D     Quit                        R           Attempt a restart</span></li><li> <span class="q"> Data Examination</span><span class="co">:</span>     <span class="w">expr</span>     <span class="w">Execute</span> <span class="w">perl</span> <span class="w">code</span><span class="cm">,</span> <span class="w">also</span> <span class="w">see</span><span class="co">:</span> <span class="q">s,n,t expr</span></li><li>  <span class="q">  x|m expr       Evals expr in list context,</span> <span class="w">dumps</span> <span class="w">the</span> <span class="w">result</span> <a class="l_k" href="functions/or.html">or</a> <span class="w">lists</span></li><li>                                                                <span class="w">methods</span>.</li><li>  <span class="w">p</span> <span class="w">expr</span>         <span class="w">Print</span> <span class="w">expression</span> <span class="s">(</span><span class="w">uses</span> <span class="w">script&#39;s</span> <span class="w">current</span> <a class="l_k" href="functions/package.html">package</a><span class="s">)</span>.</li><li>  <span class="w">S</span> <span class="s">[</span><span class="s">[</span>!<span class="s">]</span><span class="w">pat</span><span class="s">]</span>     <span class="w">List</span> <span class="w">subroutine</span> <span class="w">names</span> <span class="s">[</span><a class="l_k" href="functions/not.html">not</a><span class="s">]</span> <span class="w">matching</span> <span class="w">pattern</span></li><li>  <span class="w">V</span> <span class="s">[</span><span class="w">Pk</span> <span class="s">[</span><span class="w">Vars</span><span class="s">]</span><span class="s">]</span>  <span class="w">List</span> <span class="w">Variables</span> <span class="w">in</span> <span class="w">Package</span>.  <span class="w">Vars</span> <span class="w">can</span> <span class="w">be</span> ~<span class="w">pattern</span> <a class="l_k" href="functions/or.html">or</a></li><li>                                                               !<span class="w">pattern</span>.</li><li>  <span class="w">X</span> <span class="s">[</span><span class="w">Vars</span><span class="s">]</span>       <span class="w">Same</span> <span class="w">as</span> <span class="q">&quot;V current_package [Vars]&quot;</span>.</li><li>  <span class="q">y [n [Vars]]   List lexicals in higher scope &lt;n&gt;.  Vars same as V.</span></li><li> <span class="q"> For more help, type h cmd_letter, or run man perldebug for all docs.</span></li></ol></pre><p>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!</p>
<p>There's a couple of useful ones to know about straight away.  You wouldn't
think we're using any libraries at all at the moment, but '<b>M</b>' will show
which modules are currently loaded, and their version number, while '<b>m</b>' 
will show the methods, and '<b>S</b>' shows all subroutines (by pattern) as 
shown below.  '<b>V</b>' and '<b>X</b>' show variables in the program by package 
scope and can be constrained by pattern.</p>
<pre class="verbatim"><ol><li>	DB&lt;2&gt;S str </li><li>	dumpvar::stringify</li><li>	strict::bits</li><li>	strict::import</li><li>	strict::unimport</li></ol></pre><p>Using 'X' and cousins requires you not to use the type identifiers ($@%), just
the 'name':</p>
<pre class="verbatim"><ol><li>	DM&lt;3&gt;X ~err</li><li>	FileHandle(stderr) =&gt; fileno(2)</li></ol></pre><p>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 view some code 
at our present position (the first line of code in this case), via '<b>v</b>':</p>
<pre class="verbatim"><ol><li>	DB&lt;4&gt; v</li><li>	1       #!/usr/bin/perl</li><li>	2:      use strict;</li><li>	3</li><li>	4==&gt;    my $key = 'welcome';</li><li>	5:      my %data = (</li><li>	6               'this' =&gt; qw(that),</li><li>	7               'tom' =&gt; qw(and jerry),</li><li>	8               'welcome' =&gt; q(Hello World),</li><li>	9               'zip' =&gt; q(welcome),</li><li>	10      );</li></ol></pre><p>At line number 4 is a helpful pointer, that tells you where you are now.  To
see more code, type 'v' again:</p>
<pre class="verbatim"><ol><li>	DB&lt;4&gt; v</li><li>	8               'welcome' =&gt; q(Hello World),</li><li>	9               'zip' =&gt; q(welcome),</li><li>	10      );</li><li>	11:     my @data = keys %data;</li><li>	12:     print "All OK\n" if grep($key, keys %data);</li><li>	13:     print "$data{$key}\n";</li><li>	14:     print "done: '$data{$key}'\n";</li><li>	15:     exit;</li></ol></pre><p>And if you wanted to list line 5 again, type 'l 5', (note the space):</p>
<pre class="verbatim"><ol><li>	DB&lt;4&gt; l 5</li><li>	5:      my %data = (</li></ol></pre><p>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 '.':</p>
<pre class="verbatim"><ol><li>	DB&lt;5&gt; .</li><li>	main::(./data_a:4):     my $key = 'welcome';</li></ol></pre><p>The line shown is the one that is about to be executed <b>next</b>, it hasn't
happened yet.  So while we can print a variable with the letter '<b>p</b>', 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 '<b>s</b>':</p>
<pre class="verbatim"><ol><li>	DB&lt;6&gt; s</li><li>	main::(./data_a:5):     my %data = (</li><li>	main::(./data_a:6):             'this' =&gt; qw(that),</li><li>	main::(./data_a:7):             'tom' =&gt; qw(and jerry),</li><li>	main::(./data_a:8):             'welcome' =&gt; q(Hello World),</li><li>	main::(./data_a:9):             'zip' =&gt; q(welcome),</li><li>	main::(./data_a:10):    );</li></ol></pre><p>Now we can have a look at that first ($key) variable:</p>
<pre class="verbatim"><ol><li>	DB&lt;7&gt; p $key </li><li>	welcome</li></ol></pre><p>line 13 is where the action is, so let's continue down to there via the letter
'<b>c</b>', which by the way, inserts a 'one-time-only' breakpoint at the given
line or sub routine:</p>
<pre class="verbatim"><ol><li>	DB&lt;8&gt; c 13</li><li>	All OK</li><li>	main::(./data_a:13):    print "$data{$key}\n";</li></ol></pre><p>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:</p>
<pre class="verbatim"><ol><li>	DB&lt;9&gt; p $data{$key}</li></ol></pre><p>Not much in there, lets have a look at our hash:</p>
<pre class="verbatim"><ol><li>	DB&lt;10&gt; p %data</li><li>	Hello Worldziptomandwelcomejerrywelcomethisthat </li><li></li><li>	DB&lt;11&gt; p keys %data</li><li>	Hello Worldtomwelcomejerrythis</li></ol></pre><p>Well, this isn't very easy to read, and using the helpful manual (<b>h h</b>), the
'<b>x</b>' command looks promising:</p>
<pre class="verbatim"><ol><li>	DB&lt;12&gt; x %data</li><li>	0  'Hello World'</li><li>	1  'zip'</li><li>	2  'tom'</li><li>	3  'and'</li><li>	4  'welcome'</li><li>	5  undef</li><li>	6  'jerry'</li><li>	7  'welcome'</li><li>	8  'this'</li><li>	9  'that'</li></ol></pre><p>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 <b>reference</b>
to the data structure:</p>
<pre class="verbatim"><ol><li>	DB&lt;13&gt; x \%data</li><li>	0  HASH(0x8194bc4)</li><li>	   'Hello World' =&gt; 'zip'</li><li>	   'jerry' =&gt; 'welcome'</li><li>	   'this' =&gt; 'that'</li><li>	   'tom' =&gt; 'and'</li><li>	   'welcome' =&gt; undef</li></ol></pre><p>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.</p>
<p>The '<b>-w</b>' switch would have told us about this, had we used it at the start,
and saved us a lot of trouble:</p>
<pre class="verbatim"><ol><li>	&gt; perl -w data</li><li>	Odd number of elements in hash assignment at ./data line 5.</li></ol></pre><p>We fix our quoting: 'tom' =&gt; q(and jerry), and run it again, this time we get
our expected output:</p>
<pre class="verbatim"><ol><li>	&gt; perl -w data</li><li>	Hello World</li></ol></pre><p>While we're here, take a closer look at the '<b>x</b>' command, it's really useful
and will merrily dump out nested references, complete objects, partial objects
- just about whatever you throw at it:</p>
<p>Let's make a quick object and x-plode it, first we'll start the debugger:
it wants some form of input from STDIN, so we give it something non-committal,
a zero:</p>
<pre class="verbatim"><ol><li> &gt; <span class="w">perl</span> -<span class="w">de</span> <span class="n">0</span></li><li> <span class="w">Default</span> <a class="l_k" href="functions/die.html">die</a> <span class="w">handler</span> <span class="w">restored</span>.</li><li></li><li> <span class="w">Loading</span> <span class="w">DB</span> <span class="w">routines</span> <span class="w">from</span> <span class="w">perl5db</span>.<span class="w">pl</span> <span class="w">version</span> <span class="n">1.07</span></li><li> <span class="w">Editor</span> <span class="w">support</span> <span class="w">available</span>.</li><li></li><li> <span class="w">Enter</span> <span class="w">h</span> <a class="l_k" href="functions/or.html">or</a> <span class="q">`h h&#39; for help, or `</span><span class="w">man</span> <span class="w">perldebug</span><span class="q">&#39; for more help.</span></li><li></li><li> <span class="q"> main::(-e:1):   0</span></li></ol></pre><p>Now build an on-the-fly object over a couple of lines (note the backslash):</p>
<pre class="verbatim"><ol><li> <span class="w">DB</span><span class="q">&lt;1&gt;</span> <span class="i">$obj</span> = <a class="l_k" href="functions/bless.html">bless</a><span class="s">(</span><span class="s">{</span><span class="q">&#39;unique_id&#39;</span><span class="cm">=&gt;</span><span class="q">&#39;123&#39;</span><span class="cm">,</span> <span class="q">&#39;attr&#39;</span><span class="cm">=&gt;</span> \</li><li> <span class="w">cont</span><span class="co">:</span> 	<span class="s">{</span><span class="q">&#39;col&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;black&#39;</span><span class="cm">,</span> <span class="q">&#39;things&#39;</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="q">qw(this that etc)</span><span class="s">]</span><span class="s">}</span><span class="s">}</span><span class="cm">,</span> <span class="q">&#39;MY_class&#39;</span><span class="s">)</span></li></ol></pre><p>And let's have a look at it:</p>
<pre class="verbatim"><ol><li>  	<span class="w">DB</span><span class="q">&lt;2&gt;</span> x <span class="i">$obj</span></li><li> <span class="n">0</span>  <span class="w">MY_class</span>=<span class="i">HASH</span><span class="s">(</span><span class="n">0x828ad98</span><span class="s">)</span></li><li>   		<span class="q">&#39;attr&#39;</span> <span class="cm">=&gt;</span> <span class="i">HASH</span><span class="s">(</span><span class="n">0x828ad68</span><span class="s">)</span></li><li>      	<span class="q">&#39;col&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;black&#39;</span></li><li>      	<span class="q">&#39;things&#39;</span> <span class="cm">=&gt;</span> <span class="i">ARRAY</span><span class="s">(</span><span class="n">0x828abb8</span><span class="s">)</span></li><li>         	<span class="n">0</span>  <span class="q">&#39;this&#39;</span></li><li>         	<span class="n">1</span>  <span class="q">&#39;that&#39;</span></li><li>         	<span class="n">2</span>  <span class="q">&#39;etc&#39;</span></li><li>   		<span class="q">&#39;unique_id&#39;</span> <span class="cm">=&gt;</span> <span class="n">123</span>       </li><li>  	<span class="w">DB</span><span class="q">&lt;3&gt;</span></li></ol></pre><p>Useful, huh?  You can eval nearly anything in there, and experiment with bits
of code or regexes until the cows come home:</p>
<pre class="verbatim"><ol><li> <span class="w">DB</span><span class="q">&lt;3&gt;</span> <span class="i">@data</span> = <span class="q">qw(this that the other atheism leather theory scythe)</span></li><li></li><li> <span class="w">DB</span><span class="q">&lt;4&gt;</span> <span class="w">p</span> <span class="q">&#39;saw -&gt; &#39;</span>.<span class="s">(</span><span class="i">$cnt</span> += <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\t:\t$_\n&quot;</span> <span class="s">}</span> <a class="l_k" href="functions/grep.html">grep</a><span class="s">(</span><span class="q">/the/</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@data</span><span class="s">)</span><span class="s">)</span></li><li> <span class="w">atheism</span></li><li> <span class="w">leather</span></li><li> <span class="w">other</span></li><li> <span class="w">scythe</span></li><li> <span class="w">the</span></li><li> <span class="w">theory</span></li><li> <span class="w">saw</span> <span class="w">-&gt; 6</span></li></ol></pre><p>If you want to see the command History, type an '<b>H</b>':</p>
<pre class="verbatim"><ol><li> <span class="w">DB</span><span class="q">&lt;5&gt;</span> <span class="w">H</span></li><li> <span class="n">4</span><span class="co">:</span> <span class="w">p</span> <span class="q">&#39;saw -&gt; &#39;</span>.<span class="s">(</span><span class="i">$cnt</span> += <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;\t:\t$_\n&quot;</span> <span class="s">}</span> <a class="l_k" href="functions/grep.html">grep</a><span class="s">(</span><span class="q">/the/</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@data</span><span class="s">)</span><span class="s">)</span></li><li> <span class="n">3</span><span class="co">:</span> <span class="i">@data</span> = <span class="q">qw(this that the other atheism leather theory scythe)</span></li><li> <span class="n">2</span><span class="co">:</span> <span class="w">x</span> <span class="i">$obj</span></li><li> <span class="n">1</span><span class="co">:</span> <span class="i">$obj</span> = <a class="l_k" href="functions/bless.html">bless</a><span class="s">(</span><span class="s">{</span><span class="q">&#39;unique_id&#39;</span><span class="cm">=&gt;</span><span class="q">&#39;123&#39;</span><span class="cm">,</span> <span class="q">&#39;attr&#39;</span><span class="cm">=&gt;</span></li><li> <span class="s">{</span><span class="q">&#39;col&#39;</span> <span class="cm">=&gt;</span> <span class="q">&#39;black&#39;</span><span class="cm">,</span> <span class="q">&#39;things&#39;</span> <span class="cm">=&gt;</span> <span class="s">[</span><span class="q">qw(this that etc)</span><span class="s">]</span><span class="s">}</span><span class="s">}</span><span class="cm">,</span> <span class="q">&#39;MY_class&#39;</span><span class="s">)</span></li><li> <span class="w">DB</span><span class="q">&lt;5&gt;</span></li></ol></pre><p>And if you want to repeat any previous command, use the exclamation: '<b>!</b>':</p>
<pre class="verbatim"><ol><li> <span class="w">DB</span><span class="q">&lt;5&gt;</span> !<span class="n">4</span></li><li> <span class="w">p</span> <span class="q">&#39;saw -&gt; &#39;</span>.<span class="s">(</span><span class="i">$cnt</span> += <a class="l_k" href="functions/map.html">map</a> <span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$_\n&quot;</span> <span class="s">}</span> <a class="l_k" href="functions/grep.html">grep</a><span class="s">(</span><span class="q">/the/</span><span class="cm">,</span> <a class="l_k" href="functions/sort.html">sort</a> <span class="i">@data</span><span class="s">)</span><span class="s">)</span></li><li> <span class="w">atheism</span></li><li> <span class="w">leather</span></li><li> <span class="w">other</span></li><li> <span class="w">scythe</span></li><li> <span class="w">the</span></li><li> <span class="w">theory</span></li><li> <span class="w">saw</span> <span class="w">-&gt; 12</span></li></ol></pre><p>For more on references see <a href="perlref.html">perlref</a> and <a href="perlreftut.html">perlreftut</a></p>
<a name="Stepping-through-code"></a><h1>Stepping through code</h1>
<p>Here's a simple program which converts between Celsius and Fahrenheit, it too
has a problem:</p>
<pre class="verbatim"><ol><li> <span class="c">#!/usr/bin/perl -w</span></li><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/my.html">my</a> <span class="i">$arg</span> = <span class="i">$ARGV</span>[<span class="n">0</span>] || <span class="q">&#39;-c20&#39;</span><span class="sc">;</span></li><li></li><li> <a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$arg</span> =~ <span class="q">/^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/</span><span class="s">)</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$deg</span><span class="cm">,</span> <span class="i">$num</span><span class="s">)</span> = <span class="s">(</span><span class="i">$1</span><span class="cm">,</span> <span class="i">$2</span><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="s">(</span><span class="i">$in</span><span class="cm">,</span> <span class="i">$out</span><span class="s">)</span> = <span class="s">(</span><span class="i">$num</span><span class="cm">,</span> <span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/if.html">if</a> <span class="s">(</span><span class="i">$deg</span> <a class="l_k" href="functions/eq.html">eq</a> <span class="q">&#39;c&#39;</span><span class="s">)</span> <span class="s">{</span></li><li>		<span class="i">$deg</span> = <span class="q">&#39;f&#39;</span><span class="sc">;</span></li><li>		<span class="i">$out</span> = <span class="i">&amp;c2f</span><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>		<span class="i">$deg</span> = <span class="q">&#39;c&#39;</span><span class="sc">;</span></li><li>		<span class="i">$out</span> = <span class="i">&amp;f2c</span><span class="s">(</span><span class="i">$num</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="s">}</span></li><li>	<span class="i">$out</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="q">&#39;%0.2f&#39;</span><span class="cm">,</span> <span class="i">$out</span><span class="s">)</span><span class="sc">;</span></li><li>	<span class="i">$out</span> =~ <span class="q">s/^((\-|\+)*\d+)\.0+$/$1/</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;$out $deg\n&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span> <a class="l_k" href="functions/else.html">else</a> <span class="s">{</span></li><li>	<a class="l_k" href="functions/print.html">print</a> <span class="q">&quot;Usage: $0 -[c|f] num\n&quot;</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/exit.html">exit</a><span class="sc">;</span></li><li></li><li><a name="f2c"></a> sub <span class="m">f2c</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <span class="n">5</span> * <span class="i">$f</span> - <span class="n">32</span> / <span class="n">9</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/return.html">return</a> <span class="i">$c</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li><a name="c2f"></a> sub <span class="m">c2f</span> <span class="s">{</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$c</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span></li><li>	<a class="l_k" href="functions/my.html">my</a> <span class="i">$f</span> = <span class="n">9</span> * <span class="i">$c</span> / <span class="n">5</span> + <span class="n">32</span><span class="sc">;</span></li><li>	<a class="l_k" href="functions/return.html">return</a> <span class="i">$f</span><span class="sc">;</span></li><li> <span class="s">}</span></li></ol></pre><p>For some reason, the Fahrenheit to Celsius conversion fails to return the
expected output.  This is what it does:</p>
<pre class="verbatim"><ol><li> &gt; <span class="w">temp</span> -<span class="w">c0</span><span class="n">.72</span></li><li> <span class="n">33.30</span> <span class="w">f</span></li><li></li><li> &gt; <span class="w">temp</span> -<span class="w">f33</span><span class="n">.3</span></li><li> <span class="n">162.94</span> <span class="w">c</span></li></ol></pre><p>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.</p>
<pre class="verbatim"><ol><li>	my ($in, $out) = ($num, $num);</li><li>	$DB::single=2; # insert at line 9!</li><li>	if ($deg eq 'c') </li><li>		...</li><li></li><li>	&gt; perl -d temp -f33.3</li><li>	Default die handler restored.</li><li></li><li>	Loading DB routines from perl5db.pl version 1.07</li><li>	Editor support available.</li><li></li><li>	Enter h or `h h' for help, or `man perldebug' for more help.</li><li></li><li>	main::(temp:4): my $arg = $ARGV[0] || '-c100';</li></ol></pre><p>We'll simply continue down to our pre-set breakpoint with a '<b>c</b>':</p>
<pre class="verbatim"><ol><li>  	DB&lt;1&gt; c</li><li>	main::(temp:10):                if ($deg eq 'c') {</li></ol></pre><p>Followed by a view command to see where we are:</p>
<pre class="verbatim"><ol><li>	DB&lt;1&gt; v</li><li>	7:              my ($deg, $num) = ($1, $2);</li><li>	8:              my ($in, $out) = ($num, $num);</li><li>	9:              $DB::single=2;</li><li>	10==&gt;           if ($deg eq 'c') {</li><li>	11:                     $deg = 'f';</li><li>	12:                     $out = &amp;c2f($num);</li><li>	13              } else {</li><li>	14:                     $deg = 'c';</li><li>	15:                     $out = &amp;f2c($num);</li><li>	16              }</li></ol></pre><p>And a print to show what values we're currently using:</p>
<pre class="verbatim"><ol><li>	DB&lt;1&gt; p $deg, $num</li><li>	f33.3</li></ol></pre><p>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:</p>
<pre class="verbatim"><ol><li>	DB&lt;2&gt; b 17</li></ol></pre><p>There's no feedback from this, but you can see what breakpoints are set by
using the list 'L' command:</p>
<pre class="verbatim"><ol><li>	DB&lt;3&gt; L</li><li>	temp:</li><li> 		17:            print "$out $deg\n";</li><li>   		break if (1)</li></ol></pre><p>Note that to delete a breakpoint you use 'B'.</p>
<p>Now we'll continue down into our subroutine, this time rather than by line
number, we'll use the subroutine name, followed by the now familiar 'v':</p>
<pre class="verbatim"><ol><li>	DB&lt;3&gt; c f2c</li><li>	main::f2c(temp:30):             my $f = shift;  </li><li></li><li>	DB&lt;4&gt; v</li><li>	24:     exit;</li><li>	25</li><li>	26      sub f2c {</li><li>	27==&gt;           my $f = shift;</li><li>	28:             my $c = 5 * $f - 32 / 9; </li><li>	29:             return $c;</li><li>	30      }</li><li>	31</li><li>	32      sub c2f {</li><li>	33:             my $c = shift;</li></ol></pre><p>Note that if there was a subroutine call between us and line 29, and we wanted
to <b>single-step</b> through it, we could use the '<b>s</b>' command, and to step
over it we would use '<b>n</b>' which would execute the sub, but not descend into
it for inspection.  In this case though, we simply continue down to line 29:</p>
<pre class="verbatim"><ol><li>	DB&lt;4&gt; c 29  </li><li>	main::f2c(temp:29):             return $c;</li></ol></pre><p>And have a look at the return value:</p>
<pre class="verbatim"><ol><li>	DB&lt;5&gt; p $c</li><li>	162.944444444444</li></ol></pre><p>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:</p>
<pre class="verbatim"><ol><li>	DB&lt;6&gt; p (5 * $f - 32 / 9)</li><li>	162.944444444444</li><li></li><li>	DB&lt;7&gt; p 5 * $f - (32 / 9) </li><li>	162.944444444444</li><li></li><li>	DB&lt;8&gt; p (5 * $f) - 32 / 9</li><li>	162.944444444444</li><li></li><li>	DB&lt;9&gt; p 5 * ($f - 32) / 9</li><li>	0.722222222222221</li></ol></pre><p>:-) that's more like it!  Ok, now we can set our return variable and we'll
return out of the sub with an 'r':</p>
<pre class="verbatim"><ol><li>	DB&lt;10&gt; $c = 5 * ($f - 32) / 9</li><li></li><li>	DB&lt;11&gt; r</li><li>	scalar context return from main::f2c: 0.722222222222221</li></ol></pre><p>Looks good, let's just continue off the end of the script:</p>
<pre class="verbatim"><ol><li>	DB&lt;12&gt; c</li><li>	0.72 c </li><li>	Debugged program terminated.  Use q to quit or R to restart,</li><li>  	use O inhibit_exit to avoid stopping after program termination,</li><li>  	h q, h R or h O to get additional info.</li></ol></pre><p>A quick fix to the offending line (insert the missing parentheses) in the
actual program and we're finished.</p>
<a name="Placeholder-for-a%2c-w%2c-t%2c-T"></a><h1>Placeholder for a, w, t, T</h1>
<p>Actions, watch variables, stack traces etc.: on the TODO list.</p>
<pre class="verbatim"><ol><li>	<span class="w">a</span> </li><li></li><li>	<span class="w">w</span> </li><li></li><li>	<span class="w">t</span> </li><li></li><li>	<span class="w">T</span></li></ol></pre><a name="REGULAR-EXPRESSIONS"></a><h1>REGULAR EXPRESSIONS</h1>
<p>Ever wanted to know what a regex looked like?  You'll need perl compiled with
the DEBUGGING flag for this one:</p>
<pre class="verbatim"><ol><li>  &gt; <span class="w">perl</span> -<span class="w">Dr</span> -e <span class="q">&#39;/^pe(a)*rl$/i&#39;</span></li><li>  <span class="w">Compiling</span> <span class="w">REx</span> <span class="q">`^pe(a)*rl$&#39;</span></li><li>  <span class="q">  size 17 first at 2</span></li><li>  <span class="q">  rarest char</span></li><li>   <span class="q">   at 0</span></li><li>     <span class="q">     1: BOL(2)</span></li><li>     <span class="q">     2: EXACTF &lt;pe&gt;(4)</span></li><li>     <span class="q">     4: CURLYN[1] {0,32767}(14)</span></li><li>     <span class="q">     6:   NOTHING(8)</span></li><li>     <span class="q">     8:   EXACTF &lt;a&gt;(0)</span></li><li>    <span class="q">    12:   WHILEM(0)</span></li><li>    <span class="q">    13: NOTHING(14)</span></li><li>    <span class="q">    14: EXACTF &lt;rl&gt;(16)</span></li><li>    <span class="q">    16: EOL(17)</span></li><li>    <span class="q">    17: END(0)</span></li><li>  <span class="q">  floating `</span><span class="q">&#39;$ at 4..2147483647 (checking floating) stclass</span></li><li>    <span class="q">    `EXACTF &lt;pe&gt;&#39;</span> <span class="i">anchored</span><span class="s">(</span><span class="w">BOL</span><span class="s">)</span> <span class="w">minlen</span> <span class="n">4</span></li><li>  <span class="w">Omitting</span> <span class="i">$`</span> <span class="i">$&amp;</span> <span class="i">$&#39;</span> <span class="w">support</span>.</li><li></li><li>  <span class="w">EXECUTING</span>...</li><li></li><li>  <span class="w">Freeing</span> <span class="w">REx</span><span class="co">:</span> <span class="q">`^pe(a)*rl$&#39;</span></li></ol></pre><p>Did you really want to know? :-)
For more gory details on getting regular expressions to work, have a look at
<a href="perlre.html">perlre</a>, <a href="perlretut.html">perlretut</a>, and to decode the mysterious labels (BOL and CURLYN,
etc. above), see <a href="perldebguts.html">perldebguts</a>.</p>
<a name="OUTPUT-TIPS"></a><h1>OUTPUT TIPS</h1>
<p>To get all the output from your error log, and not miss any messages via
helpful operating system buffering, insert a line like this, at the start of
your script:</p>
<pre class="verbatim"><ol><li>	<span class="i">$|</span>=<span class="n">1</span><span class="sc">;</span></li></ol></pre><p>To watch the tail of a dynamically growing logfile, (from the command line):</p>
<pre class="verbatim"><ol><li>	<span class="w">tail</span> -f <span class="i">$error_log</span></li></ol></pre><p>Wrapping all die calls in a handler routine can be useful to see how, and from
where, they're being called, <a href="perlvar.html">perlvar</a> has more information:</p>
<pre class="verbatim"><ol><li>    <a class="l_k" href="functions/BEGIN.html">BEGIN</a> <span class="s">{</span> <span class="i">$SIG</span>{<span class="w">__DIE__</span>} = <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <a class="l_k" href="functions/require.html">require</a> <span class="w">Carp</span><span class="sc">;</span> <span class="i">Carp::confess</span><span class="s">(</span><span class="i">@_</span><span class="s">)</span> <span class="s">}</span> <span class="s">}</span></li></ol></pre><p>Various useful techniques for the redirection of STDOUT and STDERR filehandles
are explained in <a href="perlopentut.html">perlopentut</a> and <a href="perlfaq8.html">perlfaq8</a>.</p>
<a name="CGI"></a><h1>CGI</h1>
<p>Just a quick hint here for all those CGI programmers who can't figure out how
on earth to get past that 'waiting for input' prompt, when running their CGI
script from the command-line, try something like this:</p>
<pre class="verbatim"><ol><li>	&gt; perl -d my_cgi.pl -nodebug</li></ol></pre><p>Of course <a href="http://search.cpan.org/perldoc/CGI">CGI</a> and <a href="perlfaq9.html">perlfaq9</a> will tell you more.</p>
<a name="GUIs"></a><h1>GUIs</h1>
<p>The command line interface is tightly integrated with an <b>emacs</b> extension
and there's a <b>vi</b> interface too.</p>
<p>You don't have to do this all on the command line, though, there are a few GUI
options out there.  The nice thing about these is you can wave a mouse over a
variable and a dump of its data will appear in an appropriate window, or in a
popup balloon, no more tiresome typing of 'x $varname' :-)</p>
<p>In particular have a hunt around for the following:</p>
<p><b>ptkdb</b> perlTK based wrapper for the built-in debugger</p>
<p><b>ddd</b> data display debugger</p>
<p><b>PerlDevKit</b> and <b>PerlBuilder</b> are NT specific</p>
<p>NB. (more info on these and others would be appreciated).</p>
<a name="SUMMARY"></a><h1>SUMMARY</h1>
<p>We've seen how to encourage good coding practices with <b>use strict</b> and
<b>-w</b>.  We can run the perl debugger <b>perl -d scriptname</b> to inspect your
data from within the perl debugger with the <b>p</b> and <b>x</b> commands.  You can
walk through your code, set breakpoints with <b>b</b> and step through that code
with <b>s</b> or <b>n</b>, continue with <b>c</b> and return from a sub with <b>r</b>.  Fairly
intuitive stuff when you get down to it.</p>
<p>There is of course lots more to find out about, this has just scratched the
surface.  The best way to learn more is to use perldoc to find out more about
the language, to read the on-line help (<a href="perldebug.html">perldebug</a> is probably the next
place to go), and of course, experiment.</p>
<a name="SEE-ALSO"></a><h1>SEE ALSO</h1>
<p><a href="perldebug.html">perldebug</a>, 
<a href="perldebguts.html">perldebguts</a>, 
<a href="perldiag.html">perldiag</a>,
<a href="perlrun.html">perlrun</a></p>
<a name="AUTHOR"></a><h1>AUTHOR</h1>
<p>Richard Foley &lt;richard.foley@rfi.net&gt; Copyright (c) 2000</p>
<a name="CONTRIBUTORS"></a><h1>CONTRIBUTORS</h1>
<p>Various people have made helpful suggestions and contributions, in particular:</p>
<p>Ronald J Kimball &lt;rjk@linguist.dartmouth.edu&gt;</p>
<p>Hugo van der Sanden &lt;hv@crypt0.demon.co.uk&gt;</p>
<p>Peter Scott &lt;Peter@PSDT.com&gt;</p>




  <div id="page_index" class="hud_container">
    <div id="page_index_header" class="hud_header">
      <div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
      <div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
      <div id="page_index_topright" class="hud_topright"></div>
    </div>
    <div id="page_index_content" class="hud_content">
      <ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><li><a href="#use-strict">use strict</a><li><a href="#Looking-at-data-and--w-and-v">Looking at data and -w and v</a><li><a href="#help">help</a><li><a href="#Stepping-through-code">Stepping through code</a><li><a href="#Placeholder-for-a%2c-w%2c-t%2c-T">Placeholder for a, w, t, T</a><li><a href="#REGULAR-EXPRESSIONS">REGULAR EXPRESSIONS</a><li><a href="#OUTPUT-TIPS">OUTPUT TIPS</a><li><a href="#CGI">CGI</a><li><a href="#GUIs">GUIs</a><li><a href="#SUMMARY">SUMMARY</a><li><a href="#SEE-ALSO">SEE ALSO</a><li><a href="#AUTHOR">AUTHOR</a><li><a href="#CONTRIBUTORS">CONTRIBUTORS</a></ul>
    </div>
    <div id="page_index_footer" class="hud_footer">
      <div id="page_index_bottomleft" class="hud_bottomleft"></div>
      <div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
      <div id="page_index_resize" class="hud_resize"></div>
    </div>
  </div>


	    &nbsp;
          </div>
          <div id="content_footer">
          </div>
        </div>
        <div class="clear"></div>
      </div>
      
    <div id="footer">
      <div id="footer_content">
        <div id="footer_strapline">
          perldoc.perl.org - Official documentation for the Perl programming language
        </div>
        <div id="footer_links">
          <div id="address">
            <p class="name">Contact details</p>
            <p class="address">
	      Site maintained by <a href="mailto:jj@jonallen.info">Jon Allen (JJ)</a><br>
	    </p>
            <p class="contact">
              Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
            </p>
          </div>
          <ul class="f1">
            <li>Manual
              <ul class="f2">
                <li><a href="index-overview.html">Overview</a>
                <li><a href="index-tutorials.html">Tutorials</a>
                <li><a href="index-faq.html">FAQs</a>
                <li><a href="index-history.html">Changes</a>
              </ul>
            <li>Reference
              <ul class="f2">
                <li><a href="index-language.html">Language</a>
                <li><a href="index-functions.html">Functions</a>
                <li><a href="perlop.html">Operators</a>
                <li><a href="perlvar.html">Variables</a>
              </ul>
            <li>Modules
              <ul class="f2">
                <li><a href="index-modules-A.html">Modules</a>
                <li><a href="index-pragmas.html">Pragmas</a>
                <li><a href="index-utilities.html">Utilities</a>
              </ul>
            <li>Misc
              <ul class="f2">
                <li><a href="index-licence.html">License</a>
                <li><a href="index-internals.html">Internals</a>
                <li><a href="index-platforms.html">Platforms</a>
              </ul>          </ul>
          <div class="clear"></div>
        </div>
      </div>
      <div id="footer_end">
      </div>
    </div>
      
    </div>
      <script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
      <script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
  perldoc.setPath(0);
  perldoc.pageName    = 'perldebtut';
  perldoc.pageAddress = 'perldebtut.html';
  perldoc.contentPage = 1;
  explorePerl.render();
  explorePerl.addEvents('explore_anchor');
</script>
    
  </body>
</html>