Initial add of defaria.com
[clearscm.git] / defaria.com / blogs / Status / archives / 000582.html
1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 <html xmlns="http://www.w3.org/1999/xhtml" id="sixapart-standard">
4 <head>
5    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
6    <meta name="generator" content="Movable Type 5.2.3" />
7
8    <link rel="stylesheet" href="http://defaria.com/blogs/Status/styles-site.css" type="text/css" />
9    <link rel="alternate" type="application/atom+xml" title="Atom" href="http://defaria.com/blogs/Status/atom.xml" />
10    <link rel="alternate" type="application/rss+xml" title="RSS 2.0" href="http://defaria.com/blogs/Status/index.xml" />
11
12    <title>Status for Andrew DeFaria: PerlDB Tips</title>
13
14    <link rel="start" href="http://defaria.com/blogs/Status/" title="Home" />
15    <link rel="prev" href="http://defaria.com/blogs/Status/archives/000581.html" title="gpdb_add_project.pl using gpdb user and Nice" />
16    <link rel="next" href="http://defaria.com/blogs/Status/archives/000583.html" title="The Oracle speaks..." />
17
18    
19
20    
21
22    <script type="text/javascript" src="http://defaria.com/blogs/Status/mt-site.js"></script>
23 </head>
24 <body class="layout-one-column" onload="individualArchivesOnLoad(commenter_name)">
25    <div id="container">
26       <div id="container-inner" class="pkg">
27
28          <div id="banner">
29             <div id="banner-inner" class="pkg">
30                <h1 id="banner-header"><a href="http://defaria.com/blogs/Status/" accesskey="1">Status for Andrew DeFaria</a></h1>
31                <h2 id="banner-description">Searchable status reports and work log</h2>
32             </div>
33          </div>
34
35          <div id="pagebody">
36             <div id="pagebody-inner" class="pkg">
37                <div id="alpha">
38                   <div id="alpha-inner" class="pkg">
39
40                      <p class="content-nav">
41                         <a href="http://defaria.com/blogs/Status/archives/000581.html">&laquo; gpdb_add_project.pl using gpdb user and Nice</a> |
42                         <a href="http://defaria.com/blogs/Status/">Main</a>
43                         | <a href="http://defaria.com/blogs/Status/archives/000583.html">The Oracle speaks... &raquo;</a>
44                      </p>
45
46                      <a id="a000582"></a>
47                      <div class="entry" id="entry-582">
48                         <h3 class="entry-header">PerlDB Tips</h3>
49                         <div class="entry-content">
50                            <div class="entry-body">
51                               <p>The Perl debugger is one of those valuable tools that surprisingly it seems few Perl coders know well. Here are some quick tips on using the Perl debugger. First a few explanations about commands I tend to use:
52
53 <blockquote>
54 <dl>
55   <dt>s</dt>
56
57   <dd>Single step. Step to the next statement stepping into any subroutines (where the source file is known and accessible).</dd>
58
59   <dt>n</dl>
60
61   <dd>Step over - if the line contains a call to a subroutine then this will step over that subroutine.</dd>
62
63   <dt>r</dt>
64
65   <dd>Return from subroutine - if, say you accidentally stepped into a subroutine or if you just want to return,</dd>
66
67   <dt>R</dt>
68
69   <dd>Rerun - start your Perl script again in the debugger with all the parms you started with.</dd>
70
71   <dt>q</dt>
72
73   <dd>quit</dd>
74
75   <dt>p &lt;variable or expression&gt;</dt>
76
77   <dd>Will print the contents of a variable or expression. Expressions can be Perl expressions including calls to subroutines. You can, for example, do "p 'There are " . scalar @foo . ' lines in foo';</dd>
78
79   <dt>x &lt;variable or expression&gt;</dt>
80
81   <dd>Like p above however p will simply print out HASH for hashes whereas x will format them out. Also x will print out "undef" for things that are undefined yet p will print nothing for them.</dd>
82
83   <dt>l (ell)</dt>
84
85   <dd>List the next windowSize lines (see below). Use "l &lt;n&gt;"  where &lt;n&gt; = a line number to list that line.</dd>
86
87   <dt>v &lt;n&gt;</dt>
88
89   <dd>View lines around &lt;n&gt;</dd>
90
91   <dt>V &lt;package&gt;</dt>
92
93   <dd>List exported subroutines and variables for &lt;package&gt; (e.g. V MyModule will is all stuff exported from MyModule).</dd>
94
95   <dt>f &lt;filename&gt;</dt>
96
97    <dd>File - switch to another file. (e.g. f MyModule) and the debugger switches to viewing MyModule.pm.</dd>
98
99   <dt>c &lt;n&gt;</dt>
100
101   <dd>Continue to line &lt;n&gt;. If n is not specified then just continue until the next break point or the end of the script. Continue is like setting a temporary break point that disappears when you hit the line.</dd>
102
103   <dt>b &lt;n&gt; &lt;condition&gt;</dt>
104
105   <dd>Breakpoint - set a break point (or b &lt;n&gt; $name eq "Donna" which will break at line &lt;n&gt; iff $name is "Donna" (evaluated when the debugger gets to line &lt;n&gt;))
106 </dl>
107 </blockquote>
108
109 <p>Also, at the Perl db prompt you can type in any Perl. So, for example, I often work out regex's that way. I'll be debugging a Perl script and stepping up to something like:</p>
110
111 <div class=code><pre>
112      10==> if (/(\d*).*\s+/) {
113      11      print "match!\n";
114      12      $x = $1;
115      13    }
116 </pre></div>
117
118 <p>Then I'll type in stuff like:</p>
119
120 <div class=code><pre>
121      DB<10> if (/(\d*).*\s+/) { print "1 = $1\n"; } else { print "No
122      match!\n"; }
123      No match!
124      DB<11>
125 </pre></div>
126
127 <p>Then I can use the command history (with set -o emacs at the shell before executing perl db emacs key bindings work for me) to edit and enter that perl if statement changing the regex until it works correctly. This way I know I got the right regex. Copy and paste the new, tested, regex from the debugging session into my code then "R" to reload the debugger.</p>
128
129 <p>Or you can say call an arbitrary subroutine in your script:</p>
130
131 <div class=code><pre>
132        DB<2> b Rexec::ssh
133        DB<3> p Rexec::ssh
134 Rexec::ssh(/view/cmdt_x0062320/vobs/cmtools/src/misc/GPDB/bin/../../../../lib/perl/Rexec.pm:60):
135      60:         my $self = shift;
136        DB<<4>>
137 </pre></div>
138
139 <p>The "p Rexec::ssh" says to print the results of the following expression. The expression is a function call in to the Rexec module for the subroutine ssh. Since we just set a break point there in the previous debug command we break at the start of that subroutine and can then debug it. Note you don't want to "c Rexec::ssh" because that would continue the actual execution of your script and only stop at Rexec::ssh if that routine was actually called. Viola, you just forcefully caused the Perl interpreter to branch to this routine!</p>
140
141 <p>Another thing I'll frequently do is set or change variables to see how the code would proceed <b>if</b> the variables were correct (or perhaps incorrect to test error conditions). So let's say a forced execution of the subroutine Log like the above:
142
143 <div class=code><pre>
144 42      sub Log {
145 43:==>    my $msg = shift;
146 44        print "$msg\n";
147
148   DB<23> s
149 main::Log(EvilTwin.pl:45):       print "$msg\n";
150   DB<24>$msg = "Now I set msg to something I want it to be"
151   DB<25>s
152 Now I set msg to something I want it to be
153 main::Log(EvilTwin.pl:47):              return;
154   DB<25>
155 </pre></div>
156
157 <p>There are all sorts of good reasons to examine (p $variable) and set ($variable = "new value") variables during debugging.</p>
158
159 <p>Finally put the following into ~/.perldb:</p>
160
161 <div class=code><pre>
162      parse_options ("windowSize=23");
163 </pre></div>
164
165 <p>This sets the window size to 23 so that 'l" lists the next 23 lines.</p>
166                            </div>
167                            <div id="more" class="entry-more">
168                               
169                            </div>
170                         </div>
171                         <p class="entry-footer">
172                            <span class="post-footers">Posted by  on October 17, 2006  8:41 AM</span> <span class="separator">|</span> <a class="permalink" href="http://defaria.com/blogs/Status/archives/000582.html">Permalink</a>
173                         </p>
174                      </div>
175
176                      
177
178                      
179                   </div>
180                </div>
181             </div>
182          </div>
183       </div>
184    </div>
185 </body>
186 </html>