perldebtut >
5.10.0
との差分
perldebtut 5.10.0 と 5.36.0 の差分
1 | 1 | |
2 | =encoding | |
2 | =encoding utf8 | |
3 | 3 | |
4 | 4 | =head1 NAME |
5 | 5 | |
6 | 6 | =begin original |
7 | 7 | |
8 | 8 | perldebtut - Perl debugging tutorial |
9 | 9 | |
10 | 10 | =end original |
11 | 11 | |
12 | 12 | perldebtut - Perl でのデバッグのチュートリアル |
13 | 13 | |
14 | 14 | =head1 DESCRIPTION |
15 | 15 | |
16 | 16 | =begin original |
17 | 17 | |
18 | 18 | A (very) lightweight introduction in the use of the perl debugger, and a |
19 | 19 | pointer to existing, deeper sources of information on the subject of debugging |
20 | 20 | perl programs. |
21 | 21 | |
22 | 22 | =end original |
23 | 23 | |
24 | 24 | perl デバッガの使い方の(とても) 軽量な紹介および、 perl プログラムの |
25 | 25 | デバッグに関する、すでに存在するより深い情報源へのポインタです。 |
26 | 26 | |
27 | 27 | =begin original |
28 | 28 | |
29 | 29 | There's an extraordinary number of people out there who don't appear to know |
30 | 30 | anything about using the perl debugger, though they use the language every |
31 | 31 | day. |
32 | 32 | This is for them. |
33 | 33 | |
34 | 34 | =end original |
35 | 35 | |
36 | 36 | perl を毎日使っているのに、perl デバッガを使うことについて何も知らないように |
37 | 37 | 思われる人が非常にたくさんいます。 |
38 | 38 | これはそのような人たちのためのものです。 |
39 | 39 | |
40 | 40 | =head1 use strict |
41 | 41 | |
42 | 42 | =begin original |
43 | 43 | |
44 | 44 | First of all, there's a few things you can do to make your life a lot more |
45 | 45 | straightforward when it comes to debugging perl programs, without using the |
46 | 46 | debugger at all. To demonstrate, here's a simple script, named "hello", with |
47 | 47 | a problem: |
48 | 48 | |
49 | 49 | =end original |
50 | 50 | |
51 | 51 | まず最初に、perl のプログラムをデバッグするときに、デバッガを全く |
52 | 52 | 使うことなく、人生を遥かに素直なものにするためにできることがいくつか |
53 | 53 | あります。 |
54 | 54 | それを示すために、"hello" という名前の、単純ですが問題を抱えたスクリプトを |
55 | 55 | 示します: |
56 | 56 | |
57 | 57 | #!/usr/bin/perl |
58 | 58 | |
59 | 59 | $var1 = 'Hello World'; # always wanted to do that :-) |
60 | 60 | $var2 = "$varl\n"; |
61 | 61 | |
62 | 62 | print $var2; |
63 | 63 | exit; |
64 | 64 | |
65 | 65 | =begin original |
66 | 66 | |
67 | 67 | While this compiles and runs happily, it probably won't do what's expected, |
68 | 68 | namely it doesn't print "Hello World\n" at all; It will on the other hand do |
69 | 69 | exactly what it was told to do, computers being a bit that way inclined. That |
70 | 70 | is, it will print out a newline character, and you'll get what looks like a |
71 | 71 | blank line. It looks like there's 2 variables when (because of the typo) |
72 | 72 | there's really 3: |
73 | 73 | |
74 | 74 | =end original |
75 | 75 | |
76 | 76 | これはエラーなくコンパイルおよび実行されますが、おそらく想定したことは |
77 | 77 | 起きないでしょう; すなわち、"Hello World\n" とは全く表示されません; |
78 | 78 | 一方 (コンピュータに少しある傾向通りに) するように言われた通りに |
79 | 79 | 動作しています。 |
80 | 80 | これは、改行文字を表示していて、それが空行のように見えるのです。 |
81 | 81 | 2 つの変数があるように見えますが、実際には (タイプミスのために) |
82 | 82 | 3 つの変数があるのです: |
83 | 83 | |
84 | 84 | $var1 = 'Hello World'; |
85 | 85 | $varl = undef; |
86 | 86 | $var2 = "\n"; |
87 | 87 | |
88 | 88 | =begin original |
89 | 89 | |
90 | 90 | To catch this kind of problem, we can force each variable to be declared |
91 | 91 | before use by pulling in the strict module, by putting 'use strict;' after the |
92 | 92 | first line of the script. |
93 | 93 | |
94 | 94 | =end original |
95 | 95 | |
96 | 96 | この種の問題を補足するには、スクリプトの最初の行の後に 'use strict;' を |
97 | 97 | 書いて strict モジュールを導入することで、変数を使う前には宣言することを |
98 | 98 | 強制できます。 |
99 | 99 | |
100 | 100 | =begin original |
101 | 101 | |
102 | 102 | Now when you run it, perl complains about the 3 undeclared variables and we |
103 | 103 | get four error messages because one variable is referenced twice: |
104 | 104 | |
105 | 105 | =end original |
106 | 106 | |
107 | 107 | これで実行すると、perl は 3 つの未宣言変数に関して 4 つのエラーメッセージが |
108 | 108 | でます; なぜなら 1 つの変数は 2 回参照されているからです: |
109 | 109 | |
110 | 110 | Global symbol "$var1" requires explicit package name at ./t1 line 4. |
111 | 111 | Global symbol "$var2" requires explicit package name at ./t1 line 5. |
112 | 112 | Global symbol "$varl" requires explicit package name at ./t1 line 5. |
113 | 113 | Global symbol "$var2" requires explicit package name at ./t1 line 7. |
114 | 114 | Execution of ./hello aborted due to compilation errors. |
115 | 115 | |
116 | 116 | =begin original |
117 | 117 | |
118 | 118 | Luvverly! and to fix this we declare all variables explicitly and now our |
119 | 119 | script looks like this: |
120 | 120 | |
121 | 121 | =end original |
122 | 122 | |
123 | 123 | バッチリだ! |
124 | 124 | そしてこれを修正するために、全ての変数を明示的に宣言することにすると、 |
125 | 125 | スクリプトは以下のようになります: |
126 | 126 | |
127 | 127 | #!/usr/bin/perl |
128 | 128 | use strict; |
129 | 129 | |
130 | 130 | my $var1 = 'Hello World'; |
131 | 131 | my $varl = undef; |
132 | 132 | my $var2 = "$varl\n"; |
133 | 133 | |
134 | 134 | print $var2; |
135 | 135 | exit; |
136 | 136 | |
137 | 137 | =begin original |
138 | 138 | |
139 | 139 | We then do (always a good idea) a syntax check before we try to run it again: |
140 | 140 | |
141 | 141 | =end original |
142 | 142 | |
143 | 143 | それから、もう一度実行する前に文法チェックを行います(これは常に |
144 | 144 | いい考えです): |
145 | 145 | |
146 | 146 | > perl -c hello |
147 | 147 | hello syntax OK |
148 | 148 | |
149 | 149 | =begin original |
150 | 150 | |
151 | 151 | And now when we run it, we get "\n" still, but at least we know why. Just |
152 | 152 | getting this script to compile has exposed the '$varl' (with the letter 'l') |
153 | 153 | variable, and simply changing $varl to $var1 solves the problem. |
154 | 154 | |
155 | 155 | =end original |
156 | 156 | |
157 | 157 | そして実行すると、やはり "\n" が表示されますが、少なくともなぜかは |
158 | 158 | 分かります。 |
159 | 159 | コンパイルしたスクリプトに '$varl' (文字 'l' です) があることが明らかになり、 |
160 | 160 | 単に $varl を $var1 に変更すれば問題は解決します。 |
161 | 161 | |
162 | 162 | =head1 Looking at data and -w and v |
163 | 163 | |
164 | 164 | (データの見方と -w と v) |
165 | 165 | |
166 | 166 | =begin original |
167 | 167 | |
168 | 168 | Ok, but how about when you want to really see your data, what's in that |
169 | 169 | dynamic variable, just before using it? |
170 | 170 | |
171 | 171 | =end original |
172 | 172 | |
173 | 173 | よし、でも本当に、動的変数に入っているデータを、それを使う直前に知るには? |
174 | 174 | |
175 | 175 | #!/usr/bin/perl |
176 | 176 | use strict; |
177 | 177 | |
178 | 178 | my $key = 'welcome'; |
179 | 179 | my %data = ( |
180 | 180 | 'this' => qw(that), |
181 | 181 | 'tom' => qw(and jerry), |
182 | 182 | 'welcome' => q(Hello World), |
183 | 183 | 'zip' => q(welcome), |
184 | 184 | ); |
185 | 185 | my @data = keys %data; |
186 | 186 | |
187 | 187 | print "$data{$key}\n"; |
188 | 188 | exit; |
189 | 189 | |
190 | 190 | =begin original |
191 | 191 | |
192 | 192 | Looks OK, after it's been through the syntax check (perl -c scriptname), we |
193 | 193 | run it and all we get is a blank line again! Hmmmm. |
194 | 194 | |
195 | 195 | =end original |
196 | 196 | |
197 | 197 | 良さそうに見えます; 文法チェック (perl -c scriptname) の後、実行してみると、 |
198 | 198 | またも空行しか出ません! |
199 | 199 | ふーむ。 |
200 | 200 | |
201 | 201 | =begin original |
202 | 202 | |
203 | 203 | One common debugging approach here, would be to liberally sprinkle a few print |
204 | 204 | statements, to add a check just before we print out our data, and another just |
205 | 205 | after: |
206 | 206 | |
207 | 207 | =end original |
208 | 208 | |
209 | 209 | ここで一般的なデバッグ手法の一つは、print 文を自由にいくつかばらまいて、 |
210 | 210 | データをプリントする直前のチェックを追加することです: |
211 | 211 | |
212 | 212 | print "All OK\n" if grep($key, keys %data); |
213 | 213 | print "$data{$key}\n"; |
214 | 214 | print "done: '$data{$key}'\n"; |
215 | 215 | |
216 | 216 | =begin original |
217 | 217 | |
218 | 218 | And try again: |
219 | 219 | |
220 | 220 | =end original |
221 | 221 | |
222 | 222 | そして再挑戦します: |
223 | 223 | |
224 | 224 | > perl data |
225 | 225 | All OK |
226 | 226 | |
227 | 227 | done: '' |
228 | 228 | |
229 | 229 | =begin original |
230 | 230 | |
231 | 231 | After much staring at the same piece of code and not seeing the wood for the |
232 | 232 | trees for some time, we get a cup of coffee and try another approach. That |
233 | 233 | is, we bring in the cavalry by giving perl the 'B<-d>' switch on the command |
234 | 234 | line: |
235 | 235 | |
236 | 236 | =end original |
237 | 237 | |
238 | 238 | 同じコード片を見つめすぎて、木を見て森を見ずになっていることがあります; |
239 | 239 | 一服して違う手法を試しましょう。 |
240 | 240 | それは、コマンドラインで perl に 'B<-d>' オプションを与えることで騎兵隊を |
241 | 241 | 迎え入れることです: |
242 | 242 | |
243 | 243 | > perl -d data |
244 | 244 | Default die handler restored. |
245 | 245 | |
246 | 246 | Loading DB routines from perl5db.pl version 1.07 |
247 | 247 | Editor support available. |
248 | 248 | |
249 | 249 | Enter h or `h h' for help, or `man perldebug' for more help. |
250 | 250 | |
251 | 251 | main::(./data:4): my $key = 'welcome'; |
252 | 252 | |
253 | 253 | =begin original |
254 | 254 | |
255 | 255 | Now, what we've done here is to launch the built-in perl debugger on our |
256 | 256 | script. It's stopped at the first line of executable code and is waiting for |
257 | 257 | input. |
258 | 258 | |
259 | 259 | =end original |
260 | 260 | |
261 | 261 | ここでしたことは、スクリプトに対して組み込み perl デバッガを |
262 | 262 | 起動したことです。 |
263 | 263 | それは実行コードの最初の行で停止して、入力を待っています。 |
264 | 264 | |
265 | 265 | =begin original |
266 | 266 | |
267 | 267 | Before we go any further, you'll want to know how to quit the debugger: use |
268 | 268 | just the letter 'B<q>', not the words 'quit' or 'exit': |
269 | 269 | |
270 | 270 | =end original |
271 | 271 | |
272 | 272 | 先に進む前に、どうやってデバッガを抜けるかを知りたいでしょう: 単語 |
273 | 273 | 'quit' や 'exit' ではなく、単に文字 'B<q>' をタイプしてください: |
274 | 274 | |
275 | 275 | DB<1> q |
276 | 276 | > |
277 | 277 | |
278 | 278 | =begin original |
279 | 279 | |
280 | 280 | That's it, you're back on home turf again. |
281 | 281 | |
282 | 282 | =end original |
283 | 283 | |
284 | 284 | これで、再びホームグラウンドに戻ってきます。 |
285 | 285 | |
286 | 286 | =head1 help |
287 | 287 | |
288 | 288 | (ヘルプ) |
289 | 289 | |
290 | 290 | =begin original |
291 | 291 | |
292 | 292 | Fire the debugger up again on your script and we'll look at the help menu. |
293 | 293 | There's a couple of ways of calling help: a simple 'B<h>' will get the summary |
294 | 294 | help list, 'B<|h>' (pipe-h) will pipe the help through your pager (which is |
295 | 295 | (probably 'more' or 'less'), and finally, 'B<h h>' (h-space-h) will give you |
296 | 296 | the entire help screen. Here is the summary page: |
297 | 297 | |
298 | 298 | =end original |
299 | 299 | |
300 | 300 | スクリプトに対してもう一度デバッガを起動して、ヘルプメニューを見てみます。 |
301 | 301 | ヘルプを呼び出すには複数の方法があります: 単純な 'B<h>' はヘルプリストの |
302 | 302 | 要約を出力し、'B<|h>' (パイプ-h) はヘルプをページャ(多分 'more' か |
303 | 303 | 'less') に送り、最後に 'B<h h>' (h-空白-h) はヘルプスクリーン全体を |
304 | 304 | 表示します。 |
305 | 305 | 以下は要約ページです: |
306 | 306 | |
307 | 307 | DB<1>h |
308 | 308 | |
309 | 309 | List/search source lines: Control script execution: |
310 | 310 | l [ln|sub] List source code T Stack trace |
311 | - or . List previous/current line s [expr] Single step | |
311 | - or . List previous/current line s [expr] Single step | |
312 | | |
312 | [in expr] | |
313 | v [line] View around line n [expr] Next, steps over | |
314 | subs | |
313 | 315 | f filename View source in file <CR/Enter> Repeat last n or s |
314 | /pattern/ ?patt? Search forw/backw r Return from | |
316 | /pattern/ ?patt? Search forw/backw r Return from | |
315 | | |
317 | subroutine | |
316 | | |
318 | M Show module versions c [ln|sub] Continue until | |
317 | | |
319 | position | |
318 | | |
320 | Debugger controls: L List break/watch/ | |
319 | | |
321 | actions | |
322 | o [...] Set debugger options t [expr] Toggle trace | |
323 | [trace expr] | |
324 | <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set | |
325 | breakpoint | |
326 | ! [N|pat] Redo a previous command B ln|* Delete a/all | |
327 | breakpoints | |
320 | 328 | H [-num] Display last num commands a [ln] cmd Do cmd before line |
321 | = [a val] Define/list an alias A ln|* Delete a/all | |
329 | = [a val] Define/list an alias A ln|* Delete a/all | |
322 | | |
330 | actions | |
323 | h | |
331 | h [db_cmd] Get help on command w expr Add a watch | |
324 | | |
332 | expression | |
333 | h h Complete help page W expr|* Delete a/all watch | |
334 | exprs | |
335 | |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a | |
336 | subprocess | |
325 | 337 | q or ^D Quit R Attempt a restart |
326 | 338 | Data Examination: expr Execute perl code, also see: s,n,t expr |
327 | x|m expr Evals expr in list context, dumps the result or lists | |
339 | x|m expr Evals expr in list context, dumps the result or lists | |
340 | methods. | |
328 | 341 | p expr Print expression (uses script's current package). |
329 | 342 | S [[!]pat] List subroutine names [not] matching pattern |
330 | V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or | |
343 | V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or | |
344 | !pattern. | |
331 | 345 | X [Vars] Same as "V current_package [Vars]". |
332 | 346 | y [n [Vars]] List lexicals in higher scope <n>. Vars same as V. |
333 | 347 | For more help, type h cmd_letter, or run man perldebug for all docs. |
334 | 348 | |
335 | 349 | =begin original |
336 | 350 | |
337 | 351 | More confusing options than you can shake a big stick at! It's not as bad as |
338 | 352 | it looks and it's very useful to know more about all of it, and fun too! |
339 | 353 | |
340 | 354 | =end original |
341 | 355 | |
342 | 356 | とても多くの混乱させるオプションがあります! |
343 | 357 | これは見た目ほど悪くはありませんし、これらすべてについてもっと知ることは |
344 | 358 | とても有用ですし、楽しくもあります! |
345 | 359 | |
346 | 360 | =begin original |
347 | 361 | |
348 | 362 | There's a couple of useful ones to know about straight away. You wouldn't |
349 | 363 | think we're using any libraries at all at the moment, but 'B<M>' will show |
350 | 364 | which modules are currently loaded, and their version number, while 'B<m>' |
351 | 365 | will show the methods, and 'B<S>' shows all subroutines (by pattern) as |
352 | 366 | shown below. 'B<V>' and 'B<X>' show variables in the program by package |
353 | 367 | scope and can be constrained by pattern. |
354 | 368 | |
355 | 369 | =end original |
356 | 370 | |
357 | 371 | まず知っておくべきいくつかのコマンドがあります。 |
358 | 372 | この時点では何かのライブラリを使っているとは考えていないでしょうが、 |
359 | 373 | 'B<M>' は現在読み込まれているモジュールとバージョン番号を表示し、 |
360 | 374 | 一方 'B<m>' はメソッドを表示し、'B<S>' は以下のように、(パターンによって) |
361 | 375 | 全てのサブルーチンを表示します。 |
362 | 376 | 'B<V>' と'B<X>' は、パッケージスコープと、パターンによって制限できる、 |
363 | 377 | 変数を表示します。 |
364 | 378 | |
365 | 379 | DB<2>S str |
366 | 380 | dumpvar::stringify |
367 | 381 | strict::bits |
368 | 382 | strict::import |
369 | 383 | strict::unimport |
370 | 384 | |
371 | 385 | =begin original |
372 | 386 | |
373 | 387 | Using 'X' and cousins requires you not to use the type identifiers ($@%), just |
374 | 388 | the 'name': |
375 | 389 | |
376 | 390 | =end original |
377 | 391 | |
378 | 392 | X' とその親類を使う時には、型指定子($@%)を使う必要はありません; |
379 | 393 | 単に 'name' を入力してください: |
380 | 394 | |
381 | 395 | DM<3>X ~err |
382 | 396 | FileHandle(stderr) => fileno(2) |
383 | 397 | |
384 | 398 | =begin original |
385 | 399 | |
386 | 400 | Remember we're in our tiny program with a problem, we should have a look at |
387 | 401 | where we are, and what our data looks like. First of all let's view some code |
388 | 402 | at our present position (the first line of code in this case), via 'B<v>': |
389 | 403 | |
390 | 404 | =end original |
391 | 405 | |
392 | 406 | 問題を抱えた小さなプログラムがあって、今どこにいるか、そしてデータが |
393 | 407 | どのようにあっているのかを見ようとしていることを思い出してください。 |
394 | 408 | まず最初に、現在位置 (この場合ではコードの最初の行) のコードを見てみましょう; |
395 | 409 | 'B<v>' を使います: |
396 | 410 | |
397 | 411 | DB<4> v |
398 | 412 | 1 #!/usr/bin/perl |
399 | 413 | 2: use strict; |
400 | 414 | 3 |
401 | 415 | 4==> my $key = 'welcome'; |
402 | 416 | 5: my %data = ( |
403 | 417 | 6 'this' => qw(that), |
404 | 418 | 7 'tom' => qw(and jerry), |
405 | 419 | 8 'welcome' => q(Hello World), |
406 | 420 | 9 'zip' => q(welcome), |
407 | 421 | 10 ); |
408 | 422 | |
409 | 423 | =begin original |
410 | 424 | |
411 | 425 | At line number 4 is a helpful pointer, that tells you where you are now. To |
412 | 426 | see more code, type 'v' again: |
413 | 427 | |
414 | 428 | =end original |
415 | 429 | |
416 | 430 | 行番号 4 にあるのは助けになるポインタで、今どこにいるのかを示しています。 |
417 | 431 | さらにコードを見るには、再び 'v' をタイプします: |
418 | 432 | |
419 | 433 | DB<4> v |
420 | 434 | 8 'welcome' => q(Hello World), |
421 | 435 | 9 'zip' => q(welcome), |
422 | 436 | 10 ); |
423 | 437 | 11: my @data = keys %data; |
424 | 438 | 12: print "All OK\n" if grep($key, keys %data); |
425 | 439 | 13: print "$data{$key}\n"; |
426 | 440 | 14: print "done: '$data{$key}'\n"; |
427 | 441 | 15: exit; |
428 | 442 | |
429 | 443 | =begin original |
430 | 444 | |
431 | 445 | And if you wanted to list line 5 again, type 'l 5', (note the space): |
432 | 446 | |
433 | 447 | =end original |
434 | 448 | |
435 | 449 | そしてもし行番号 5 を再び見たいなら、'l 5' をタイプします |
436 | 450 | (空白に注意してください): |
437 | 451 | |
438 | 452 | DB<4> l 5 |
439 | 453 | 5: my %data = ( |
440 | 454 | |
441 | 455 | =begin original |
442 | 456 | |
443 | 457 | In this case, there's not much to see, but of course normally there's pages of |
444 | 458 | stuff to wade through, and 'l' can be very useful. To reset your view to the |
445 | 459 | line we're about to execute, type a lone period '.': |
446 | 460 | |
447 | 461 | =end original |
448 | 462 | |
449 | 463 | この場合、見られるものはあまり多くはありませんが、もちろん普通は見渡すのに |
450 | 464 | 何ページにもなる内容があるので、'l' はとても有用です。 |
451 | 465 | 見ている場所を実行しようとしているところにリセットするには、単一の '.' を |
452 | 466 | タイプします: |
453 | 467 | |
454 | 468 | DB<5> . |
455 | 469 | main::(./data_a:4): my $key = 'welcome'; |
456 | 470 | |
457 | 471 | =begin original |
458 | 472 | |
459 | 473 | The line shown is the one that is about to be executed B<next>, it hasn't |
460 | 474 | happened yet. So while we can print a variable with the letter 'B<p>', at |
461 | 475 | this point all we'd get is an empty (undefined) value back. What we need to |
462 | 476 | do is to step through the next executable statement with an 'B<s>': |
463 | 477 | |
464 | 478 | =end original |
465 | 479 | |
466 | 480 | 表示されている行は B<次に> 実行されようとしているもので、まだ |
467 | 481 | 実行されていません。 |
468 | 482 | 従って、ここで文字 'B<p>' を使って変数を表示できますが、この時点では |
469 | 483 | 表示されるのは空(未定義)値だけです。 |
470 | 484 | するべきことは、'B<s>' を使って次の実行可能文に進むことです: |
471 | 485 | |
472 | 486 | DB<6> s |
473 | 487 | main::(./data_a:5): my %data = ( |
474 | 488 | main::(./data_a:6): 'this' => qw(that), |
475 | 489 | main::(./data_a:7): 'tom' => qw(and jerry), |
476 | 490 | main::(./data_a:8): 'welcome' => q(Hello World), |
477 | 491 | main::(./data_a:9): 'zip' => q(welcome), |
478 | 492 | main::(./data_a:10): ); |
479 | 493 | |
480 | 494 | =begin original |
481 | 495 | |
482 | 496 | Now we can have a look at that first ($key) variable: |
483 | 497 | |
484 | 498 | =end original |
485 | 499 | |
486 | 500 | ここで最初の ($key) 変数を見ることができます: |
487 | 501 | |
488 | 502 | DB<7> p $key |
489 | 503 | welcome |
490 | 504 | |
491 | 505 | =begin original |
492 | 506 | |
493 | 507 | line 13 is where the action is, so let's continue down to there via the letter |
494 | 508 | 'B<c>', which by the way, inserts a 'one-time-only' breakpoint at the given |
495 | 509 | line or sub routine: |
496 | 510 | |
497 | 511 | =end original |
498 | 512 | |
499 | 513 | 行 13 が処理の場所なので、文字 'B<c>' を使って、今度は「一回だけ」の |
500 | 514 | ブレークポイントを与えられた行かサブルーチンに挿入することでそこまで |
501 | 515 | 進めていきましょう: |
502 | 516 | |
503 | 517 | DB<8> c 13 |
504 | 518 | All OK |
505 | 519 | main::(./data_a:13): print "$data{$key}\n"; |
506 | 520 | |
507 | 521 | =begin original |
508 | 522 | |
509 | 523 | We've gone past our check (where 'All OK' was printed) and have stopped just |
510 | 524 | before the meat of our task. We could try to print out a couple of variables |
511 | 525 | to see what is happening: |
512 | 526 | |
513 | 527 | =end original |
514 | 528 | |
515 | 529 | チェック('All OK' が表示された場所)を通り過ぎて、作業の要点の直線で |
516 | 530 | 停止しました。 |
517 | 531 | 何が起きているのかを見るために二つの変数を表示させようとすることが |
518 | 532 | できます: |
519 | 533 | |
520 | 534 | DB<9> p $data{$key} |
521 | 535 | |
522 | 536 | =begin original |
523 | 537 | |
524 | 538 | Not much in there, lets have a look at our hash: |
525 | 539 | |
526 | 540 | =end original |
527 | 541 | |
528 | 542 | あまりありませんが、ハッシュを見てみましょう: |
529 | 543 | |
530 | 544 | DB<10> p %data |
531 | 545 | Hello Worldziptomandwelcomejerrywelcomethisthat |
532 | 546 | |
533 | 547 | DB<11> p keys %data |
534 | 548 | Hello Worldtomwelcomejerrythis |
535 | 549 | |
536 | 550 | =begin original |
537 | 551 | |
538 | 552 | Well, this isn't very easy to read, and using the helpful manual (B<h h>), the |
539 | 553 | 'B<x>' command looks promising: |
540 | 554 | |
541 | 555 | =end original |
542 | 556 | |
543 | 557 | うーん、これはとても読みやすいというものではありません; そして親切な |
544 | 558 | マニュアル (B<h h>) を使うと、'B<x>' コマンドが見込みがありそうです: |
545 | 559 | |
546 | 560 | DB<12> x %data |
547 | 561 | 0 'Hello World' |
548 | 562 | 1 'zip' |
549 | 563 | 2 'tom' |
550 | 564 | 3 'and' |
551 | 565 | 4 'welcome' |
552 | 566 | 5 undef |
553 | 567 | 6 'jerry' |
554 | 568 | 7 'welcome' |
555 | 569 | 8 'this' |
556 | 570 | 9 'that' |
557 | 571 | |
558 | 572 | =begin original |
559 | 573 | |
560 | 574 | That's not much help, a couple of welcomes in there, but no indication of |
561 | 575 | which are keys, and which are values, it's just a listed array dump and, in |
562 | 576 | this case, not particularly helpful. The trick here, is to use a B<reference> |
563 | 577 | to the data structure: |
564 | 578 | |
565 | 579 | =end original |
566 | 580 | |
567 | 581 | これはあまり助けにはなりません; ここには 2 つの "welcome" がありますが、 |
568 | 582 | どれがキーでどれが値かを示すものがなく、単に配列ダンプの一覧で、 |
569 | 583 | この場合、特に役に立つものではありません。 |
570 | 584 | ここでの技は、データ構造への B<リファレンス> を使うことです: |
571 | 585 | |
572 | 586 | DB<13> x \%data |
573 | 587 | 0 HASH(0x8194bc4) |
574 | 588 | 'Hello World' => 'zip' |
575 | 589 | 'jerry' => 'welcome' |
576 | 590 | 'this' => 'that' |
577 | 591 | 'tom' => 'and' |
578 | 592 | 'welcome' => undef |
579 | 593 | |
580 | 594 | =begin original |
581 | 595 | |
582 | 596 | The reference is truly dumped and we can finally see what we're dealing with. |
583 | 597 | Our quoting was perfectly valid but wrong for our purposes, with 'and jerry' |
584 | 598 | being treated as 2 separate words rather than a phrase, thus throwing the |
585 | 599 | evenly paired hash structure out of alignment. |
586 | 600 | |
587 | 601 | =end original |
588 | 602 | |
589 | 603 | リファレンスが完全にダンプされて、ついに扱っているものが見えました。 |
590 | 604 | クォートは完全に有効でしたが、今回の目的には間違ったものでした; |
591 | 605 | 'and jerry' が熟語ではなく、2 つの別々の単語として扱われています; |
592 | 606 | 従って、2 つ組のハッシュ構造のアライメントがずれたのです。 |
593 | 607 | |
594 | 608 | =begin original |
595 | 609 | |
596 | 610 | The 'B<-w>' switch would have told us about this, had we used it at the start, |
597 | 611 | and saved us a lot of trouble: |
598 | 612 | |
599 | 613 | =end original |
600 | 614 | |
601 | 615 | 'B<-w>' オプションはこれについて教えてくれるので、最初に使っておけば、 |
602 | 616 | 多くの問題から救ってくれていました: |
603 | 617 | |
604 | 618 | > perl -w data |
605 | 619 | Odd number of elements in hash assignment at ./data line 5. |
606 | 620 | |
607 | 621 | =begin original |
608 | 622 | |
609 | 623 | We fix our quoting: 'tom' => q(and jerry), and run it again, this time we get |
610 | 624 | our expected output: |
611 | 625 | |
612 | 626 | =end original |
613 | 627 | |
614 | 628 | クォートを修正します: 'tom' => q(and jerry)、そして再実行すると、今度は |
615 | 629 | 予想通りの出力が得られます: |
616 | 630 | |
617 | 631 | > perl -w data |
618 | 632 | Hello World |
619 | 633 | |
620 | ||
621 | 634 | =begin original |
622 | 635 | |
623 | 636 | While we're here, take a closer look at the 'B<x>' command, it's really useful |
624 | 637 | and will merrily dump out nested references, complete objects, partial objects |
625 | 638 | - just about whatever you throw at it: |
626 | 639 | |
627 | 640 | =end original |
628 | 641 | |
629 | 642 | ここにいる間に 'B<x>' コマンドをより近くで見てみると、これは本当に有用で、 |
630 | 643 | ネストしたリファレンス、完全なオブジェクト、オブジェクトの一部 - コマンドに |
631 | 644 | 与えたものは何でも - 楽しくダンプします: |
632 | 645 | |
633 | 646 | =begin original |
634 | 647 | |
635 | 648 | Let's make a quick object and x-plode it, first we'll start the debugger: |
636 | 649 | it wants some form of input from STDIN, so we give it something non-committal, |
637 | 650 | a zero: |
638 | 651 | |
639 | 652 | =end original |
640 | 653 | |
641 | 654 | 簡単なオブジェクトを作って、見てみましょう; まずデバッガを起動します: |
642 | 655 | これは STDIN から何らかの形の入力を要求するので、何か無害なもの - ゼロ - を |
643 | 656 | 入力します: |
644 | 657 | |
645 | ||
658 | > perl -de 0 | |
646 | ||
659 | Default die handler restored. | |
647 | 660 | |
648 | ||
661 | Loading DB routines from perl5db.pl version 1.07 | |
649 | ||
662 | Editor support available. | |
650 | 663 | |
651 | ||
664 | Enter h or `h h' for help, or `man perldebug' for more help. | |
652 | 665 | |
653 | ||
666 | main::(-e:1): 0 | |
654 | 667 | |
655 | 668 | =begin original |
656 | 669 | |
657 | 670 | Now build an on-the-fly object over a couple of lines (note the backslash): |
658 | 671 | |
659 | 672 | =end original |
660 | 673 | |
661 | 674 | ここで、複数行を使ってその場でオブジェクトを構築します |
662 | 675 | (バックスラッシュに注意してください): |
663 | 676 | |
664 | ||
677 | DB<1> $obj = bless({'unique_id'=>'123', 'attr'=> \ | |
665 | ||
678 | cont: {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class') | |
666 | 679 | |
667 | 680 | =begin original |
668 | 681 | |
669 | 682 | And let's have a look at it: |
670 | 683 | |
671 | 684 | =end original |
672 | 685 | |
673 | 686 | そして見てみましょう: |
674 | 687 | |
675 | 688 | DB<2> x $obj |
676 | ||
689 | 0 MY_class=HASH(0x828ad98) | |
677 | 690 | 'attr' => HASH(0x828ad68) |
678 | 691 | 'col' => 'black' |
679 | 692 | 'things' => ARRAY(0x828abb8) |
680 | 693 | 0 'this' |
681 | 694 | 1 'that' |
682 | 695 | 2 'etc' |
683 | 696 | 'unique_id' => 123 |
684 | 697 | DB<3> |
685 | 698 | |
686 | 699 | =begin original |
687 | 700 | |
688 | 701 | Useful, huh? You can eval nearly anything in there, and experiment with bits |
689 | 702 | of code or regexes until the cows come home: |
690 | 703 | |
691 | 704 | =end original |
692 | 705 | |
693 | 706 | 便利でしょう? |
694 | 707 | ここでほとんどなんでも eval できて、ちょっとしたコードや正規表現を |
695 | 708 | いつまでも実験できます。 |
696 | 709 | |
697 | ||
710 | DB<3> @data = qw(this that the other atheism leather theory scythe) | |
698 | 711 | |
699 | ||
712 | DB<4> p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data)) | |
700 | ||
713 | atheism | |
701 | ||
714 | leather | |
702 | ||
715 | other | |
703 | ||
716 | scythe | |
704 | ||
717 | the | |
705 | ||
718 | theory | |
706 | ||
719 | saw -> 6 | |
707 | 720 | |
708 | 721 | =begin original |
709 | 722 | |
710 | 723 | If you want to see the command History, type an 'B<H>': |
711 | 724 | |
712 | 725 | =end original |
713 | 726 | |
714 | 727 | コマンド履歴を見たいなら、'B<H>' をタイプします: |
715 | 728 | |
716 | ||
729 | DB<5> H | |
717 | ||
730 | 4: p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data)) | |
718 | ||
731 | 3: @data = qw(this that the other atheism leather theory scythe) | |
719 | ||
732 | 2: x $obj | |
720 | ||
733 | 1: $obj = bless({'unique_id'=>'123', 'attr'=> | |
721 | ||
734 | {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class') | |
722 | ||
735 | DB<5> | |
723 | 736 | |
724 | 737 | =begin original |
725 | 738 | |
726 | 739 | And if you want to repeat any previous command, use the exclamation: 'B<!>': |
727 | 740 | |
728 | 741 | =end original |
729 | 742 | |
730 | 743 | 以前に使ったコマンドを繰り返したい場合は、感嘆符を使います: 'B<!>': |
731 | 744 | |
732 | ||
745 | DB<5> !4 | |
733 | ||
746 | p 'saw -> '.($cnt += map { print "$_\n" } grep(/the/, sort @data)) | |
734 | ||
747 | atheism | |
735 | ||
748 | leather | |
736 | ||
749 | other | |
737 | ||
750 | scythe | |
738 | ||
751 | the | |
739 | ||
752 | theory | |
740 | ||
753 | saw -> 12 | |
741 | 754 | |
742 | 755 | =begin original |
743 | 756 | |
744 | 757 | For more on references see L<perlref> and L<perlreftut> |
745 | 758 | |
746 | 759 | =end original |
747 | 760 | |
748 | 761 | リファレンスについてのさらなる情報については L<perlref> と L<perlreftut> を |
749 | 762 | 参照してください。 |
750 | 763 | |
751 | 764 | =head1 Stepping through code |
752 | 765 | |
753 | 766 | (コードをステップ実行する) |
754 | 767 | |
755 | 768 | =begin original |
756 | 769 | |
757 | 770 | Here's a simple program which converts between Celsius and Fahrenheit, it too |
758 | 771 | has a problem: |
759 | 772 | |
760 | 773 | =end original |
761 | 774 | |
762 | 775 | 以下は摂氏と華氏とを変換する単純なプログラムで、やはり問題を抱えています: |
763 | 776 | |
764 | ||
777 | #!/usr/bin/perl | |
765 | ||
778 | use v5.36; | |
766 | 779 | |
767 | ||
780 | my $arg = $ARGV[0] || '-c20'; | |
768 | 781 | |
769 | ||
782 | if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) { | |
770 | | |
783 | my ($deg, $num) = ($1, $2); | |
771 | | |
784 | my ($in, $out) = ($num, $num); | |
772 | | |
785 | if ($deg eq 'c') { | |
773 | | |
786 | $deg = 'f'; | |
774 | | |
787 | $out = &c2f($num); | |
775 | } else { | |
776 | $deg = 'c'; | |
777 | $out = &f2c($num); | |
778 | } | |
779 | $out = sprintf('%0.2f', $out); | |
780 | $out =~ s/^((\-|\+)*\d+)\.0+$/$1/; | |
781 | print "$out $deg\n"; | |
782 | 788 | } else { |
783 | | |
789 | $deg = 'c'; | |
790 | $out = &f2c($num); | |
784 | 791 | } |
785 | | |
792 | $out = sprintf('%0.2f', $out); | |
793 | $out =~ s/^((\-|\+)*\d+)\.0+$/$1/; | |
794 | print "$out $deg\n"; | |
795 | } else { | |
796 | print "Usage: $0 -[c|f] num\n"; | |
797 | } | |
798 | exit; | |
786 | 799 | |
787 | ||
800 | sub f2c { | |
788 | | |
801 | my $f = shift; | |
789 | | |
802 | my $c = 5 * $f - 32 / 9; | |
790 | | |
803 | return $c; | |
791 | ||
804 | } | |
792 | 805 | |
793 | ||
806 | sub c2f { | |
794 | | |
807 | my $c = shift; | |
795 | | |
808 | my $f = 9 * $c / 5 + 32; | |
796 | | |
809 | return $f; | |
797 | ||
810 | } | |
798 | 811 | |
799 | ||
800 | 812 | =begin original |
801 | 813 | |
802 | 814 | For some reason, the Fahrenheit to Celsius conversion fails to return the |
803 | 815 | expected output. This is what it does: |
804 | 816 | |
805 | 817 | =end original |
806 | 818 | |
807 | 819 | なぜか、華氏から摂氏への変換は推測される結果を返すのに失敗します。 |
808 | 820 | 以下はどうなるかです: |
809 | 821 | |
810 | ||
822 | > temp -c0.72 | |
811 | ||
823 | 33.30 f | |
812 | 824 | |
813 | ||
825 | > temp -f33.3 | |
814 | ||
826 | 162.94 c | |
815 | 827 | |
816 | 828 | =begin original |
817 | 829 | |
818 | 830 | Not very consistent! We'll set a breakpoint in the code manually and run it |
819 | 831 | under the debugger to see what's going on. A breakpoint is a flag, to which |
820 | 832 | the debugger will run without interruption, when it reaches the breakpoint, it |
821 | 833 | will stop execution and offer a prompt for further interaction. In normal |
822 | 834 | use, these debugger commands are completely ignored, and they are safe - if a |
823 | 835 | little messy, to leave in production code. |
824 | 836 | |
825 | 837 | =end original |
826 | 838 | |
827 | 839 | 全く一貫していません! |
828 | 840 | 手動でコードにブレークポイントをセットして、何が起きているかを見るために |
829 | 841 | デバッガで実行してみます。 |
830 | 842 | ブレークポイントは、デバッガを中断なしで実行するためのフラグで、 |
831 | 843 | ブレークポイントに到達すると、実行を停止してさらなる対話のためにプロンプトを |
832 | 844 | 出します。 |
833 | 845 | 通常の使用では、これらのデバッガコマンドは完全に無視され、これらは |
834 | 846 | 製品コードに残しても安全です - すこし乱雑かもしれませんが。 |
835 | 847 | |
836 | 848 | my ($in, $out) = ($num, $num); |
837 | 849 | $DB::single=2; # insert at line 9! |
838 | 850 | if ($deg eq 'c') |
839 | 851 | ... |
840 | 852 | |
841 | 853 | > perl -d temp -f33.3 |
842 | 854 | Default die handler restored. |
843 | 855 | |
844 | 856 | Loading DB routines from perl5db.pl version 1.07 |
845 | 857 | Editor support available. |
846 | 858 | |
847 | 859 | Enter h or `h h' for help, or `man perldebug' for more help. |
848 | 860 | |
849 | 861 | main::(temp:4): my $arg = $ARGV[0] || '-c100'; |
850 | 862 | |
851 | 863 | =begin original |
852 | 864 | |
853 | 865 | We'll simply continue down to our pre-set breakpoint with a 'B<c>': |
854 | 866 | |
855 | 867 | =end original |
856 | 868 | |
857 | 869 | 'B<c>' をタイプして、単純に予めセットされたブレークポイントまで続けます: |
858 | 870 | |
859 | 871 | DB<1> c |
860 | 872 | main::(temp:10): if ($deg eq 'c') { |
861 | 873 | |
862 | 874 | =begin original |
863 | 875 | |
864 | 876 | Followed by a view command to see where we are: |
865 | 877 | |
866 | 878 | =end original |
867 | 879 | |
868 | 880 | 引き続いて表示コマンドで今どこにいるかを見ます: |
869 | 881 | |
870 | 882 | DB<1> v |
871 | 883 | 7: my ($deg, $num) = ($1, $2); |
872 | 884 | 8: my ($in, $out) = ($num, $num); |
873 | 885 | 9: $DB::single=2; |
874 | 886 | 10==> if ($deg eq 'c') { |
875 | 887 | 11: $deg = 'f'; |
876 | 888 | 12: $out = &c2f($num); |
877 | 889 | 13 } else { |
878 | 890 | 14: $deg = 'c'; |
879 | 891 | 15: $out = &f2c($num); |
880 | 892 | 16 } |
881 | 893 | |
882 | 894 | =begin original |
883 | 895 | |
884 | 896 | And a print to show what values we're currently using: |
885 | 897 | |
886 | 898 | =end original |
887 | 899 | |
888 | 900 | そして今使っている値を表示させます: |
889 | 901 | |
890 | 902 | DB<1> p $deg, $num |
891 | 903 | f33.3 |
892 | 904 | |
893 | 905 | =begin original |
894 | 906 | |
895 | 907 | We can put another break point on any line beginning with a colon, we'll use |
896 | 908 | line 17 as that's just as we come out of the subroutine, and we'd like to |
897 | 909 | pause there later on: |
898 | 910 | |
899 | 911 | =end original |
900 | 912 | |
901 | 913 | コロンの付いているどの行にも別のブレークポイントを置くことができます; |
902 | 914 | サブルーチンから返ってきたばかりのところである 17 行目を使うことにして、 |
903 | 915 | あとからここで一旦停止したいとします: |
904 | 916 | |
905 | 917 | DB<2> b 17 |
906 | 918 | |
907 | 919 | =begin original |
908 | 920 | |
909 | 921 | There's no feedback from this, but you can see what breakpoints are set by |
910 | 922 | using the list 'L' command: |
911 | 923 | |
912 | 924 | =end original |
913 | 925 | |
914 | 926 | これに対する反応はありませんが、一覧 'L' コマンドを使うことで、どの |
915 | 927 | ブレークポイントがセットされているかを見ることができます: |
916 | 928 | |
917 | 929 | DB<3> L |
918 | 930 | temp: |
919 | 931 | 17: print "$out $deg\n"; |
920 | 932 | break if (1) |
921 | 933 | |
922 | 934 | =begin original |
923 | 935 | |
924 | Note that to delete a breakpoint you use ' | |
936 | Note that to delete a breakpoint you use 'B'. | |
925 | 937 | |
926 | 938 | =end original |
927 | 939 | |
928 | ブレークポイントを削除するためには ' | |
940 | ブレークポイントを削除するためには 'B' を使うことに注意してください。 | |
929 | 注意してください。 | |
930 | 941 | |
931 | 942 | =begin original |
932 | 943 | |
933 | 944 | Now we'll continue down into our subroutine, this time rather than by line |
934 | 945 | number, we'll use the subroutine name, followed by the now familiar 'v': |
935 | 946 | |
936 | 947 | =end original |
937 | 948 | |
938 | 949 | ここでサブルーチンの中に入っていくことにします; 今回は行番号ではなく、 |
939 | 950 | サブルーチン名を使います; その後、今となってはおなじみの 'v' を使います: |
940 | 951 | |
941 | 952 | DB<3> c f2c |
942 | 953 | main::f2c(temp:30): my $f = shift; |
943 | 954 | |
944 | 955 | DB<4> v |
945 | 956 | 24: exit; |
946 | 957 | 25 |
947 | 958 | 26 sub f2c { |
948 | 959 | 27==> my $f = shift; |
949 | 960 | 28: my $c = 5 * $f - 32 / 9; |
950 | 961 | 29: return $c; |
951 | 962 | 30 } |
952 | 963 | 31 |
953 | 964 | 32 sub c2f { |
954 | 965 | 33: my $c = shift; |
955 | 966 | |
956 | ||
957 | 967 | =begin original |
958 | 968 | |
959 | 969 | Note that if there was a subroutine call between us and line 29, and we wanted |
960 | 970 | to B<single-step> through it, we could use the 'B<s>' command, and to step |
961 | 971 | over it we would use 'B<n>' which would execute the sub, but not descend into |
962 | 972 | it for inspection. In this case though, we simply continue down to line 29: |
963 | 973 | |
964 | 974 | =end original |
965 | 975 | |
966 | 976 | ここと 29 行目との間にサブルーチンがあり、そこを B<シングルステップ> したい |
967 | 977 | 場合、'B<s>' コマンドも使えますし、サブルーチンは実行するけれども |
968 | 978 | サブルーチン内部は検査しないという 'B<n>' コマンドで |
969 | 979 | ステップオーバーできます。 |
970 | 980 | しかし、この場合には、単に 29 行まで進めていきます: |
971 | 981 | |
972 | 982 | DB<4> c 29 |
973 | 983 | main::f2c(temp:29): return $c; |
974 | 984 | |
975 | 985 | =begin original |
976 | 986 | |
977 | 987 | And have a look at the return value: |
978 | 988 | |
979 | 989 | =end original |
980 | 990 | |
981 | 991 | そして返り値を見てみます: |
982 | 992 | |
983 | 993 | DB<5> p $c |
984 | 994 | 162.944444444444 |
985 | 995 | |
986 | 996 | =begin original |
987 | 997 | |
988 | 998 | This is not the right answer at all, but the sum looks correct. I wonder if |
989 | 999 | it's anything to do with operator precedence? We'll try a couple of other |
990 | 1000 | possibilities with our sum: |
991 | 1001 | |
992 | 1002 | =end original |
993 | 1003 | |
994 | 1004 | これは全く間違った答えですが、合計は正しいように見えます。 |
995 | 1005 | 演算子の優先順位が何かを行っているのでしょうか? |
996 | 1006 | 合計に関してその他の可能性を試してみます: |
997 | 1007 | |
998 | 1008 | DB<6> p (5 * $f - 32 / 9) |
999 | 1009 | 162.944444444444 |
1000 | 1010 | |
1001 | 1011 | DB<7> p 5 * $f - (32 / 9) |
1002 | 1012 | 162.944444444444 |
1003 | 1013 | |
1004 | 1014 | DB<8> p (5 * $f) - 32 / 9 |
1005 | 1015 | 162.944444444444 |
1006 | 1016 | |
1007 | 1017 | DB<9> p 5 * ($f - 32) / 9 |
1008 | 1018 | 0.722222222222221 |
1009 | 1019 | |
1010 | 1020 | =begin original |
1011 | 1021 | |
1012 | 1022 | :-) that's more like it! Ok, now we can set our return variable and we'll |
1013 | 1023 | return out of the sub with an 'r': |
1014 | 1024 | |
1015 | 1025 | =end original |
1016 | 1026 | |
1017 | 1027 | :-) これはより似ています! |
1018 | 1028 | よし、ここで独自の返り値をセットして、'r' を使ってサブルーチンから返ります: |
1019 | 1029 | |
1020 | 1030 | DB<10> $c = 5 * ($f - 32) / 9 |
1021 | 1031 | |
1022 | 1032 | DB<11> r |
1023 | 1033 | scalar context return from main::f2c: 0.722222222222221 |
1024 | 1034 | |
1025 | 1035 | =begin original |
1026 | 1036 | |
1027 | 1037 | Looks good, let's just continue off the end of the script: |
1028 | 1038 | |
1029 | 1039 | =end original |
1030 | 1040 | |
1031 | 1041 | 良さそうです; スクリプトの最後まで実行していましょう: |
1032 | 1042 | |
1033 | 1043 | DB<12> c |
1034 | 1044 | 0.72 c |
1035 | 1045 | Debugged program terminated. Use q to quit or R to restart, |
1036 | 1046 | use O inhibit_exit to avoid stopping after program termination, |
1037 | 1047 | h q, h R or h O to get additional info. |
1038 | 1048 | |
1039 | 1049 | =begin original |
1040 | 1050 | |
1041 | 1051 | A quick fix to the offending line (insert the missing parentheses) in the |
1042 | 1052 | actual program and we're finished. |
1043 | 1053 | |
1044 | 1054 | =end original |
1045 | 1055 | |
1046 | 1056 | 実際のプログラムの問題のある行に救急処置(不足していたかっこを挿入する)を |
1047 | 1057 | 施して、終わりです。 |
1048 | 1058 | |
1049 | 1059 | =head1 Placeholder for a, w, t, T |
1050 | 1060 | |
1051 | 1061 | (a, w, t, T のためのプレースホルダ) |
1052 | 1062 | |
1053 | 1063 | =begin original |
1054 | 1064 | |
1055 | 1065 | Actions, watch variables, stack traces etc.: on the TODO list. |
1056 | 1066 | |
1057 | 1067 | =end original |
1058 | 1068 | |
1059 | 1069 | アクション、変数の監視、スタックトレースなど: TODO リストです。 |
1060 | 1070 | |
1061 | 1071 | a |
1062 | 1072 | |
1063 | 1073 | w |
1064 | 1074 | |
1065 | 1075 | t |
1066 | 1076 | |
1067 | 1077 | T |
1068 | 1078 | |
1069 | ||
1070 | 1079 | =head1 REGULAR EXPRESSIONS |
1071 | 1080 | |
1072 | 1081 | (正規表現) |
1073 | 1082 | |
1074 | 1083 | =begin original |
1075 | 1084 | |
1076 | 1085 | Ever wanted to know what a regex looked like? You'll need perl compiled with |
1077 | 1086 | the DEBUGGING flag for this one: |
1078 | 1087 | |
1079 | 1088 | =end original |
1080 | 1089 | |
1081 | 1090 | 正規表現がどのように見えるか知りたいと思いましたか? |
1082 | 1091 | 以下のようにするには perl を DEBUGGING フラグ付きでコンパイルする必要が |
1083 | 1092 | あります: |
1084 | 1093 | |
1085 | ||
1094 | > perl -Dr -e '/^pe(a)*rl$/i' | |
1086 | ||
1095 | Compiling REx `^pe(a)*rl$' | |
1087 | ||
1096 | size 17 first at 2 | |
1088 | ||
1097 | rarest char | |
1089 | ||
1098 | at 0 | |
1090 | ||
1099 | 1: BOL(2) | |
1091 | ||
1100 | 2: EXACTF <pe>(4) | |
1092 | ||
1101 | 4: CURLYN[1] {0,32767}(14) | |
1093 | ||
1102 | 6: NOTHING(8) | |
1094 | ||
1103 | 8: EXACTF <a>(0) | |
1095 | ||
1104 | 12: WHILEM(0) | |
1096 | ||
1105 | 13: NOTHING(14) | |
1097 | ||
1106 | 14: EXACTF <rl>(16) | |
1098 | ||
1107 | 16: EOL(17) | |
1099 | ||
1108 | 17: END(0) | |
1100 | ||
1109 | floating `'$ at 4..2147483647 (checking floating) stclass | |
1101 | anchored(BOL) minlen 4 | |
1110 | `EXACTF <pe>' anchored(BOL) minlen 4 | |
1102 | ||
1111 | Omitting $` $& $' support. | |
1103 | 1112 | |
1104 | ||
1113 | EXECUTING... | |
1105 | 1114 | |
1106 | ||
1115 | Freeing REx: `^pe(a)*rl$' | |
1107 | 1116 | |
1108 | 1117 | =begin original |
1109 | 1118 | |
1110 | 1119 | Did you really want to know? :-) |
1111 | 1120 | For more gory details on getting regular expressions to work, have a look at |
1112 | 1121 | L<perlre>, L<perlretut>, and to decode the mysterious labels (BOL and CURLYN, |
1113 | 1122 | etc. above), see L<perldebguts>. |
1114 | 1123 | |
1115 | 1124 | =end original |
1116 | 1125 | |
1117 | 1126 | 本当に知りたかったですか? :-) |
1118 | 1127 | 正規表現の動作に関する詳細については、L<perlre>, L<perlretut> を、 |
1119 | 1128 | (上述の BOL や CURLYN などの)不思議なラベルを解読するには、 |
1120 | 1129 | L<perldebguts> を参照してください。 |
1121 | 1130 | |
1122 | 1131 | =head1 OUTPUT TIPS |
1123 | 1132 | |
1124 | 1133 | (出力の小技) |
1125 | 1134 | |
1126 | 1135 | =begin original |
1127 | 1136 | |
1128 | 1137 | To get all the output from your error log, and not miss any messages via |
1129 | 1138 | helpful operating system buffering, insert a line like this, at the start of |
1130 | 1139 | your script: |
1131 | 1140 | |
1132 | 1141 | =end original |
1133 | 1142 | |
1134 | 1143 | エラーログからの全ての出力を得て、親切な OS のバッファリングで |
1135 | 1144 | メッセージを失わないようにするには、スクリプトの最初に以下のような行を |
1136 | 1145 | 挿入してください: |
1137 | 1146 | |
1138 | 1147 | $|=1; |
1139 | 1148 | |
1140 | 1149 | =begin original |
1141 | 1150 | |
1142 | 1151 | To watch the tail of a dynamically growing logfile, (from the command line): |
1143 | 1152 | |
1144 | 1153 | =end original |
1145 | 1154 | |
1146 | 1155 | 動的に増え続けるログファイルの末尾を監視するには、(コマンドラインから): |
1147 | 1156 | |
1148 | 1157 | tail -f $error_log |
1149 | 1158 | |
1150 | 1159 | =begin original |
1151 | 1160 | |
1152 | 1161 | Wrapping all die calls in a handler routine can be useful to see how, and from |
1153 | 1162 | where, they're being called, L<perlvar> has more information: |
1154 | 1163 | |
1155 | 1164 | =end original |
1156 | 1165 | |
1157 | 1166 | 全ての die 呼び出しをハンドラルーチンで囲むと、どこで、どのように |
1158 | 1167 | 呼び出されているかを知るのに有用です; L<perlvar> にさらなる情報があります: |
1159 | 1168 | |
1160 | ||
1169 | BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } } | |
1161 | 1170 | |
1162 | 1171 | =begin original |
1163 | 1172 | |
1164 | 1173 | Various useful techniques for the redirection of STDOUT and STDERR filehandles |
1165 | 1174 | are explained in L<perlopentut> and L<perlfaq8>. |
1166 | 1175 | |
1167 | 1176 | =end original |
1168 | 1177 | |
1169 | 1178 | STDOUT と STDERR ファイルハンドルのリダイレクトに関する様々な便利な |
1170 | 1179 | テクニックが L<perlopentut> と L<perlfaq8> に記述されています。 |
1171 | 1180 | |
1172 | 1181 | =head1 CGI |
1173 | 1182 | |
1174 | 1183 | =begin original |
1175 | 1184 | |
1176 | 1185 | Just a quick hint here for all those CGI programmers who can't figure out how |
1177 | 1186 | on earth to get past that 'waiting for input' prompt, when running their CGI |
1178 | 1187 | script from the command-line, try something like this: |
1179 | 1188 | |
1180 | 1189 | =end original |
1181 | 1190 | |
1182 | 1191 | 「入力待ち」プロンプトからどうやれば逃れられるのかが分からない全ての |
1183 | 1192 | CGI プログラマへの簡単なヒントとして、 |
1184 | 1193 | CGI をコマンドラインから実行するときに、以下のようなものを試してください: |
1185 | 1194 | |
1186 | 1195 | > perl -d my_cgi.pl -nodebug |
1187 | 1196 | |
1188 | 1197 | =begin original |
1189 | 1198 | |
1190 | 1199 | Of course L<CGI> and L<perlfaq9> will tell you more. |
1191 | 1200 | |
1192 | 1201 | =end original |
1193 | 1202 | |
1194 | 1203 | もちろん L<CGI> と L<perlfaq9> にはもっと多くの情報があります。 |
1195 | 1204 | |
1196 | 1205 | =head1 GUIs |
1197 | 1206 | |
1198 | 1207 | (GUI) |
1199 | 1208 | |
1200 | 1209 | =begin original |
1201 | 1210 | |
1202 | 1211 | The command line interface is tightly integrated with an B<emacs> extension |
1203 | 1212 | and there's a B<vi> interface too. |
1204 | 1213 | |
1205 | 1214 | =end original |
1206 | 1215 | |
1207 | 1216 | コマンドラインインターフェースは B<emacs> 拡張と密接に統合されていて、 |
1208 | 1217 | B<vi> インターフェースもあります。 |
1209 | 1218 | |
1210 | 1219 | =begin original |
1211 | 1220 | |
1212 | 1221 | You don't have to do this all on the command line, though, there are a few GUI |
1213 | 1222 | options out there. The nice thing about these is you can wave a mouse over a |
1214 | 1223 | variable and a dump of its data will appear in an appropriate window, or in a |
1215 | 1224 | popup balloon, no more tiresome typing of 'x $varname' :-) |
1216 | 1225 | |
1217 | 1226 | =end original |
1218 | 1227 | |
1219 | 1228 | しかし、これら全てをコマンドラインで実行する必要はありません; |
1220 | 1229 | いくつかの GUI の選択肢もあります。 |
1221 | 1230 | これらのよいところは、マウスカーソルを変数の上に移動させると適切な |
1222 | 1231 | ウィンドウやバルーンにそのデータがダンプされ、もう 'x $varname' と |
1223 | 1232 | タイプしなくていいことです :-) |
1224 | 1233 | |
1225 | 1234 | =begin original |
1226 | 1235 | |
1227 | 1236 | In particular have a hunt around for the following: |
1228 | 1237 | |
1229 | 1238 | =end original |
1230 | 1239 | |
1231 | 1240 | 特に以下のものの辺りを調べてみてください: |
1232 | 1241 | |
1233 | 1242 | =begin original |
1234 | 1243 | |
1235 | 1244 | B<ptkdb> perlTK based wrapper for the built-in debugger |
1236 | 1245 | |
1237 | 1246 | =end original |
1238 | 1247 | |
1239 | 1248 | B<ptkdb> ビルドインデバッガのための perlTK ベースのラッパー |
1240 | 1249 | |
1241 | 1250 | =begin original |
1242 | 1251 | |
1243 | 1252 | B<ddd> data display debugger |
1244 | 1253 | |
1245 | 1254 | =end original |
1246 | 1255 | |
1247 | 1256 | B<ddd> データ表示デバッガ |
1248 | 1257 | |
1249 | 1258 | =begin original |
1250 | 1259 | |
1251 | 1260 | B<PerlDevKit> and B<PerlBuilder> are NT specific |
1252 | 1261 | |
1253 | 1262 | =end original |
1254 | 1263 | |
1255 | 1264 | B<PerlDevKit> と B<PerlBuilder> は NT 固有です |
1256 | 1265 | |
1257 | 1266 | =begin original |
1258 | 1267 | |
1259 | 1268 | NB. (more info on these and others would be appreciated). |
1260 | 1269 | |
1261 | 1270 | =end original |
1262 | 1271 | |
1263 | 1272 | 注意せよ。 |
1264 | 1273 | (これらやその他のものに関するさらなる情報を頂ければ幸いです)。 |
1265 | 1274 | |
1266 | 1275 | =head1 SUMMARY |
1267 | 1276 | |
1268 | 1277 | (まとめ) |
1269 | 1278 | |
1270 | 1279 | =begin original |
1271 | 1280 | |
1272 | 1281 | We've seen how to encourage good coding practices with B<use strict> and |
1273 | 1282 | B<-w>. We can run the perl debugger B<perl -d scriptname> to inspect your |
1274 | 1283 | data from within the perl debugger with the B<p> and B<x> commands. You can |
1275 | 1284 | walk through your code, set breakpoints with B<b> and step through that code |
1276 | 1285 | with B<s> or B<n>, continue with B<c> and return from a sub with B<r>. Fairly |
1277 | 1286 | intuitive stuff when you get down to it. |
1278 | 1287 | |
1279 | 1288 | =end original |
1280 | 1289 | |
1281 | 1290 | B<use strict> と B<-w> を使ってどうやって良いコーディングを実践するかを |
1282 | 1291 | 見ました。 |
1283 | 1292 | B<perl -d scriptname> とすることで perl デバッガを起動でき、デバッガの |
1284 | 1293 | B<p> や B<x> のコマンドでデータを検査できます。 |
1285 | 1294 | コードの中を通り抜けて、B<b> でブレークポイントを設定し、 |
1286 | 1295 | B<s> や B<n> でステップ実行を行い、B<c> で再開して、B<r> サブルーチンから |
1287 | 1296 | 戻ります。 |
1288 | 1297 | うんざりしたときにはかなり直感的な機能です。 |
1289 | 1298 | |
1290 | 1299 | =begin original |
1291 | 1300 | |
1292 | 1301 | There is of course lots more to find out about, this has just scratched the |
1293 | 1302 | surface. The best way to learn more is to use perldoc to find out more about |
1294 | 1303 | the language, to read the on-line help (L<perldebug> is probably the next |
1295 | 1304 | place to go), and of course, experiment. |
1296 | 1305 | |
1297 | 1306 | =end original |
1298 | 1307 | |
1299 | 1308 | もちろんもっと多くの調べるべきことがありますが、これは表面を |
1300 | 1309 | なぞっただけです。 |
1301 | 1310 | より多くを学ぶための最善の方法は、言語に関してより多くを調べるために |
1302 | 1311 | オンラインヘルプを読むために perldoc を使う(おそらく次に進むべき |
1303 | 1312 | ところは L<perldebug> でしょう)ことと、もちろん実践です。 |
1304 | 1313 | |
1305 | 1314 | =head1 SEE ALSO |
1306 | 1315 | |
1307 | 1316 | L<perldebug>, |
1308 | 1317 | L<perldebguts>, |
1318 | L<perl5db.pl>, | |
1309 | 1319 | L<perldiag>, |
1310 | L<dprofpp>, | |
1311 | 1320 | L<perlrun> |
1312 | 1321 | |
1313 | 1322 | =head1 AUTHOR |
1314 | 1323 | |
1315 | Richard Foley <richard@rfi.net> Copyright (c) 2000 | |
1324 | Richard Foley <richard.foley@rfi.net> Copyright (c) 2000 | |
1316 | 1325 | |
1317 | 1326 | =head1 CONTRIBUTORS |
1318 | 1327 | |
1319 | 1328 | (貢献者) |
1320 | 1329 | |
1321 | 1330 | =begin original |
1322 | 1331 | |
1323 | 1332 | Various people have made helpful suggestions and contributions, in particular: |
1324 | 1333 | |
1325 | 1334 | =end original |
1326 | 1335 | |
1327 | 1336 | 様々な人々が有益な提案や貢献をしてくれました; 特に: |
1328 | 1337 | |
1329 | 1338 | Ronald J Kimball <rjk@linguist.dartmouth.edu> |
1330 | 1339 | |
1331 | 1340 | Hugo van der Sanden <hv@crypt0.demon.co.uk> |
1332 | 1341 | |
1333 | 1342 | Peter Scott <Peter@PSDT.com> |
1334 | 1343 | |
1335 | 1344 | =begin meta |
1336 | 1345 | |
1337 | Translate: Kentaro | |
1346 | Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-) | |
1347 | Status: completed | |
1338 | 1348 | |
1339 | 1349 | =end meta |