perldebtut > 5.10.0 との差分

perldebtut 5.10.0 と 5.36.0 の差分

11
2=encoding euc-jp
2=encoding utf8
33
44=head1 NAME
55
66=begin original
77
88perldebtut - Perl debugging tutorial
99
1010=end original
1111
1212perldebtut - Perl でのデバッグのチュートリアル
1313
1414=head1 DESCRIPTION
1515
1616=begin original
1717
1818A (very) lightweight introduction in the use of the perl debugger, and a
1919pointer to existing, deeper sources of information on the subject of debugging
2020perl programs.
2121
2222=end original
2323
2424perl デバッガの使い方の(とても) 軽量な紹介および、 perl プログラムの
2525デバッグに関する、すでに存在するより深い情報源へのポインタです。
2626
2727=begin original
2828
2929There's an extraordinary number of people out there who don't appear to know
3030anything about using the perl debugger, though they use the language every
3131day.
3232This is for them.
3333
3434=end original
3535
3636perl を毎日使っているのに、perl デバッガを使うことについて何も知らないように
3737思われる人が非常にたくさんいます。
3838これはそのような人たちのためのものです。
3939
4040=head1 use strict
4141
4242=begin original
4343
4444First of all, there's a few things you can do to make your life a lot more
4545straightforward when it comes to debugging perl programs, without using the
4646debugger at all. To demonstrate, here's a simple script, named "hello", with
4747a problem:
4848
4949=end original
5050
5151まず最初に、perl のプログラムをデバッグするときに、デバッガを全く
5252使うことなく、人生を遥かに素直なものにするためにできることがいくつか
5353あります。
5454それを示すために、"hello" という名前の、単純ですが問題を抱えたスクリプトを
5555示します:
5656
5757 #!/usr/bin/perl
5858
5959 $var1 = 'Hello World'; # always wanted to do that :-)
6060 $var2 = "$varl\n";
6161
6262 print $var2;
6363 exit;
6464
6565=begin original
6666
6767While this compiles and runs happily, it probably won't do what's expected,
6868namely it doesn't print "Hello World\n" at all; It will on the other hand do
6969exactly what it was told to do, computers being a bit that way inclined. That
7070is, it will print out a newline character, and you'll get what looks like a
7171blank line. It looks like there's 2 variables when (because of the typo)
7272there's really 3:
7373
7474=end original
7575
7676これはエラーなくコンパイルおよび実行されますが、おそらく想定したことは
7777起きないでしょう; すなわち、"Hello World\n" とは全く表示されません;
7878一方 (コンピュータに少しある傾向通りに) するように言われた通りに
7979動作しています。
8080これは、改行文字を表示していて、それが空行のように見えるのです。
81812 つの変数があるように見えますが、実際には (タイプミスのために)
82823 つの変数があるのです:
8383
8484 $var1 = 'Hello World';
8585 $varl = undef;
8686 $var2 = "\n";
8787
8888=begin original
8989
9090To catch this kind of problem, we can force each variable to be declared
9191before use by pulling in the strict module, by putting 'use strict;' after the
9292first line of the script.
9393
9494=end original
9595
9696この種の問題を補足するには、スクリプトの最初の行の後に 'use strict;' を
9797書いて strict モジュールを導入することで、変数を使う前には宣言することを
9898強制できます。
9999
100100=begin original
101101
102102Now when you run it, perl complains about the 3 undeclared variables and we
103103get four error messages because one variable is referenced twice:
104104
105105=end original
106106
107107これで実行すると、perl は 3 つの未宣言変数に関して 4 つのエラーメッセージが
108108でます; なぜなら 1 つの変数は 2 回参照されているからです:
109109
110110 Global symbol "$var1" requires explicit package name at ./t1 line 4.
111111 Global symbol "$var2" requires explicit package name at ./t1 line 5.
112112 Global symbol "$varl" requires explicit package name at ./t1 line 5.
113113 Global symbol "$var2" requires explicit package name at ./t1 line 7.
114114 Execution of ./hello aborted due to compilation errors.
115115
116116=begin original
117117
118118Luvverly! and to fix this we declare all variables explicitly and now our
119119script looks like this:
120120
121121=end original
122122
123123バッチリだ!
124124そしてこれを修正するために、全ての変数を明示的に宣言することにすると、
125125スクリプトは以下のようになります:
126126
127127 #!/usr/bin/perl
128128 use strict;
129129
130130 my $var1 = 'Hello World';
131131 my $varl = undef;
132132 my $var2 = "$varl\n";
133133
134134 print $var2;
135135 exit;
136136
137137=begin original
138138
139139We then do (always a good idea) a syntax check before we try to run it again:
140140
141141=end original
142142
143143それから、もう一度実行する前に文法チェックを行います(これは常に
144144いい考えです):
145145
146146 > perl -c hello
147147 hello syntax OK
148148
149149=begin original
150150
151151And now when we run it, we get "\n" still, but at least we know why. Just
152152getting this script to compile has exposed the '$varl' (with the letter 'l')
153153variable, and simply changing $varl to $var1 solves the problem.
154154
155155=end original
156156
157157そして実行すると、やはり "\n" が表示されますが、少なくともなぜかは
158158分かります。
159159コンパイルしたスクリプトに '$varl' (文字 'l' です) があることが明らかになり、
160160単に $varl を $var1 に変更すれば問題は解決します。
161161
162162=head1 Looking at data and -w and v
163163
164164(データの見方と -w と v)
165165
166166=begin original
167167
168168Ok, but how about when you want to really see your data, what's in that
169169dynamic variable, just before using it?
170170
171171=end original
172172
173173よし、でも本当に、動的変数に入っているデータを、それを使う直前に知るには?
174174
175175 #!/usr/bin/perl
176176 use strict;
177177
178178 my $key = 'welcome';
179179 my %data = (
180180 'this' => qw(that),
181181 'tom' => qw(and jerry),
182182 'welcome' => q(Hello World),
183183 'zip' => q(welcome),
184184 );
185185 my @data = keys %data;
186186
187187 print "$data{$key}\n";
188188 exit;
189189
190190=begin original
191191
192192Looks OK, after it's been through the syntax check (perl -c scriptname), we
193193run it and all we get is a blank line again! Hmmmm.
194194
195195=end original
196196
197197良さそうに見えます; 文法チェック (perl -c scriptname) の後、実行してみると、
198198またも空行しか出ません!
199199ふーむ。
200200
201201=begin original
202202
203203One common debugging approach here, would be to liberally sprinkle a few print
204204statements, to add a check just before we print out our data, and another just
205205after:
206206
207207=end original
208208
209209ここで一般的なデバッグ手法の一つは、print 文を自由にいくつかばらまいて、
210210データをプリントする直前のチェックを追加することです:
211211
212212 print "All OK\n" if grep($key, keys %data);
213213 print "$data{$key}\n";
214214 print "done: '$data{$key}'\n";
215215
216216=begin original
217217
218218And try again:
219219
220220=end original
221221
222222そして再挑戦します:
223223
224224 > perl data
225225 All OK
226226
227227 done: ''
228228
229229=begin original
230230
231231After much staring at the same piece of code and not seeing the wood for the
232232trees for some time, we get a cup of coffee and try another approach. That
233233is, we bring in the cavalry by giving perl the 'B<-d>' switch on the command
234234line:
235235
236236=end original
237237
238238同じコード片を見つめすぎて、木を見て森を見ずになっていることがあります;
239239一服して違う手法を試しましょう。
240240それは、コマンドラインで perl に 'B<-d>' オプションを与えることで騎兵隊を
241241迎え入れることです:
242242
243243 > perl -d data
244244 Default die handler restored.
245245
246246 Loading DB routines from perl5db.pl version 1.07
247247 Editor support available.
248248
249249 Enter h or `h h' for help, or `man perldebug' for more help.
250250
251251 main::(./data:4): my $key = 'welcome';
252252
253253=begin original
254254
255255Now, what we've done here is to launch the built-in perl debugger on our
256256script. It's stopped at the first line of executable code and is waiting for
257257input.
258258
259259=end original
260260
261261ここでしたことは、スクリプトに対して組み込み perl デバッガを
262262起動したことです。
263263それは実行コードの最初の行で停止して、入力を待っています。
264264
265265=begin original
266266
267267Before we go any further, you'll want to know how to quit the debugger: use
268268just the letter 'B<q>', not the words 'quit' or 'exit':
269269
270270=end original
271271
272272先に進む前に、どうやってデバッガを抜けるかを知りたいでしょう: 単語
273273'quit' や 'exit' ではなく、単に文字 'B<q>' をタイプしてください:
274274
275275 DB<1> q
276276 >
277277
278278=begin original
279279
280280That's it, you're back on home turf again.
281281
282282=end original
283283
284284これで、再びホームグラウンドに戻ってきます。
285285
286286=head1 help
287287
288288(ヘルプ)
289289
290290=begin original
291291
292292Fire the debugger up again on your script and we'll look at the help menu.
293293There's a couple of ways of calling help: a simple 'B<h>' will get the summary
294294help list, 'B<|h>' (pipe-h) will pipe the help through your pager (which is
295295(probably 'more' or 'less'), and finally, 'B<h h>' (h-space-h) will give you
296296the entire help screen. Here is the summary page:
297297
298298=end original
299299
300300スクリプトに対してもう一度デバッガを起動して、ヘルプメニューを見てみます。
301301ヘルプを呼び出すには複数の方法があります: 単純な 'B<h>' はヘルプリストの
302302要約を出力し、'B<|h>' (パイプ-h) はヘルプをページャ(多分 'more' か
303303'less') に送り、最後に 'B<h h>' (h-空白-h) はヘルプスクリーン全体を
304304表示します。
305305以下は要約ページです:
306306
307307DB<1>h
308308
309309 List/search source lines: Control script execution:
310310 l [ln|sub] List source code T Stack trace
311 - or . List previous/current line s [expr] Single step [in expr]
311 - or . List previous/current line s [expr] Single step
312 v [line] View around line n [expr] Next, steps over subs
312 [in expr]
313 v [line] View around line n [expr] Next, steps over
314 subs
313315 f filename View source in file <CR/Enter> Repeat last n or s
314 /pattern/ ?patt? Search forw/backw r Return from subroutine
316 /pattern/ ?patt? Search forw/backw r Return from
315 M Show module versions c [ln|sub] Continue until position
317 subroutine
316 Debugger controls: L List break/watch/actions
318 M Show module versions c [ln|sub] Continue until
317 o [...] Set debugger options t [expr] Toggle trace [trace expr]
319 position
318 <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint
320 Debugger controls: L List break/watch/
319 ! [N|pat] Redo a previous command B ln|* Delete a/all breakpoints
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
320328 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 actions
329 = [a val] Define/list an alias A ln|* Delete a/all
322 h [db_cmd] Get help on command w expr Add a watch expression
330 actions
323 h h Complete help page W expr|* Delete a/all watch exprs
331 h [db_cmd] Get help on command w expr Add a watch
324 |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess
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
325337 q or ^D Quit R Attempt a restart
326338 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 methods.
339 x|m expr Evals expr in list context, dumps the result or lists
340 methods.
328341 p expr Print expression (uses script's current package).
329342 S [[!]pat] List subroutine names [not] matching pattern
330 V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern.
343 V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or
344 !pattern.
331345 X [Vars] Same as "V current_package [Vars]".
332346 y [n [Vars]] List lexicals in higher scope <n>. Vars same as V.
333347 For more help, type h cmd_letter, or run man perldebug for all docs.
334348
335349=begin original
336350
337351More confusing options than you can shake a big stick at! It's not as bad as
338352it looks and it's very useful to know more about all of it, and fun too!
339353
340354=end original
341355
342356とても多くの混乱させるオプションがあります!
343357これは見た目ほど悪くはありませんし、これらすべてについてもっと知ることは
344358とても有用ですし、楽しくもあります!
345359
346360=begin original
347361
348362There's a couple of useful ones to know about straight away. You wouldn't
349363think we're using any libraries at all at the moment, but 'B<M>' will show
350364which modules are currently loaded, and their version number, while 'B<m>'
351365will show the methods, and 'B<S>' shows all subroutines (by pattern) as
352366shown below. 'B<V>' and 'B<X>' show variables in the program by package
353367scope and can be constrained by pattern.
354368
355369=end original
356370
357371まず知っておくべきいくつかのコマンドがあります。
358372この時点では何かのライブラリを使っているとは考えていないでしょうが、
359373'B<M>' は現在読み込まれているモジュールとバージョン番号を表示し、
360374一方 'B<m>' はメソッドを表示し、'B<S>' は以下のように、(パターンによって)
361375全てのサブルーチンを表示します。
362376'B<V>' と'B<X>' は、パッケージスコープと、パターンによって制限できる、
363377変数を表示します。
364378
365379 DB<2>S str
366380 dumpvar::stringify
367381 strict::bits
368382 strict::import
369383 strict::unimport
370384
371385=begin original
372386
373387Using 'X' and cousins requires you not to use the type identifiers ($@%), just
374388the 'name':
375389
376390=end original
377391
378392X' とその親類を使う時には、型指定子($@%)を使う必要はありません;
379393単に 'name' を入力してください:
380394
381395 DM<3>X ~err
382396 FileHandle(stderr) => fileno(2)
383397
384398=begin original
385399
386400Remember we're in our tiny program with a problem, we should have a look at
387401where we are, and what our data looks like. First of all let's view some code
388402at our present position (the first line of code in this case), via 'B<v>':
389403
390404=end original
391405
392406問題を抱えた小さなプログラムがあって、今どこにいるか、そしてデータが
393407どのようにあっているのかを見ようとしていることを思い出してください。
394408まず最初に、現在位置 (この場合ではコードの最初の行) のコードを見てみましょう;
395409'B<v>' を使います:
396410
397411 DB<4> v
398412 1 #!/usr/bin/perl
399413 2: use strict;
400414 3
401415 4==> my $key = 'welcome';
402416 5: my %data = (
403417 6 'this' => qw(that),
404418 7 'tom' => qw(and jerry),
405419 8 'welcome' => q(Hello World),
406420 9 'zip' => q(welcome),
407421 10 );
408422
409423=begin original
410424
411425At line number 4 is a helpful pointer, that tells you where you are now. To
412426see more code, type 'v' again:
413427
414428=end original
415429
416430行番号 4 にあるのは助けになるポインタで、今どこにいるのかを示しています。
417431さらにコードを見るには、再び 'v' をタイプします:
418432
419433 DB<4> v
420434 8 'welcome' => q(Hello World),
421435 9 'zip' => q(welcome),
422436 10 );
423437 11: my @data = keys %data;
424438 12: print "All OK\n" if grep($key, keys %data);
425439 13: print "$data{$key}\n";
426440 14: print "done: '$data{$key}'\n";
427441 15: exit;
428442
429443=begin original
430444
431445And if you wanted to list line 5 again, type 'l 5', (note the space):
432446
433447=end original
434448
435449そしてもし行番号 5 を再び見たいなら、'l 5' をタイプします
436450(空白に注意してください):
437451
438452 DB<4> l 5
439453 5: my %data = (
440454
441455=begin original
442456
443457In this case, there's not much to see, but of course normally there's pages of
444458stuff to wade through, and 'l' can be very useful. To reset your view to the
445459line we're about to execute, type a lone period '.':
446460
447461=end original
448462
449463この場合、見られるものはあまり多くはありませんが、もちろん普通は見渡すのに
450464何ページにもなる内容があるので、'l' はとても有用です。
451465見ている場所を実行しようとしているところにリセットするには、単一の '.' を
452466タイプします:
453467
454468 DB<5> .
455469 main::(./data_a:4): my $key = 'welcome';
456470
457471=begin original
458472
459473The line shown is the one that is about to be executed B<next>, it hasn't
460474happened yet. So while we can print a variable with the letter 'B<p>', at
461475this point all we'd get is an empty (undefined) value back. What we need to
462476do is to step through the next executable statement with an 'B<s>':
463477
464478=end original
465479
466480表示されている行は B<次に> 実行されようとしているもので、まだ
467481実行されていません。
468482従って、ここで文字 'B<p>' を使って変数を表示できますが、この時点では
469483表示されるのは空(未定義)値だけです。
470484するべきことは、'B<s>' を使って次の実行可能文に進むことです:
471485
472486 DB<6> s
473487 main::(./data_a:5): my %data = (
474488 main::(./data_a:6): 'this' => qw(that),
475489 main::(./data_a:7): 'tom' => qw(and jerry),
476490 main::(./data_a:8): 'welcome' => q(Hello World),
477491 main::(./data_a:9): 'zip' => q(welcome),
478492 main::(./data_a:10): );
479493
480494=begin original
481495
482496Now we can have a look at that first ($key) variable:
483497
484498=end original
485499
486500ここで最初の ($key) 変数を見ることができます:
487501
488502 DB<7> p $key
489503 welcome
490504
491505=begin original
492506
493507line 13 is where the action is, so let's continue down to there via the letter
494508'B<c>', which by the way, inserts a 'one-time-only' breakpoint at the given
495509line or sub routine:
496510
497511=end original
498512
499513行 13 が処理の場所なので、文字 'B<c>' を使って、今度は「一回だけ」の
500514ブレークポイントを与えられた行かサブルーチンに挿入することでそこまで
501515進めていきましょう:
502516
503517 DB<8> c 13
504518 All OK
505519 main::(./data_a:13): print "$data{$key}\n";
506520
507521=begin original
508522
509523We've gone past our check (where 'All OK' was printed) and have stopped just
510524before the meat of our task. We could try to print out a couple of variables
511525to see what is happening:
512526
513527=end original
514528
515529チェック('All OK' が表示された場所)を通り過ぎて、作業の要点の直線で
516530停止しました。
517531何が起きているのかを見るために二つの変数を表示させようとすることが
518532できます:
519533
520534 DB<9> p $data{$key}
521535
522536=begin original
523537
524538Not much in there, lets have a look at our hash:
525539
526540=end original
527541
528542あまりありませんが、ハッシュを見てみましょう:
529543
530544 DB<10> p %data
531545 Hello Worldziptomandwelcomejerrywelcomethisthat
532546
533547 DB<11> p keys %data
534548 Hello Worldtomwelcomejerrythis
535549
536550=begin original
537551
538552Well, this isn't very easy to read, and using the helpful manual (B<h h>), the
539553'B<x>' command looks promising:
540554
541555=end original
542556
543557うーん、これはとても読みやすいというものではありません; そして親切な
544558マニュアル (B<h h>) を使うと、'B<x>' コマンドが見込みがありそうです:
545559
546560 DB<12> x %data
547561 0 'Hello World'
548562 1 'zip'
549563 2 'tom'
550564 3 'and'
551565 4 'welcome'
552566 5 undef
553567 6 'jerry'
554568 7 'welcome'
555569 8 'this'
556570 9 'that'
557571
558572=begin original
559573
560574That's not much help, a couple of welcomes in there, but no indication of
561575which are keys, and which are values, it's just a listed array dump and, in
562576this case, not particularly helpful. The trick here, is to use a B<reference>
563577to the data structure:
564578
565579=end original
566580
567581これはあまり助けにはなりません; ここには 2 つの "welcome" がありますが、
568582どれがキーでどれが値かを示すものがなく、単に配列ダンプの一覧で、
569583この場合、特に役に立つものではありません。
570584ここでの技は、データ構造への B<リファレンス> を使うことです:
571585
572586 DB<13> x \%data
573587 0 HASH(0x8194bc4)
574588 'Hello World' => 'zip'
575589 'jerry' => 'welcome'
576590 'this' => 'that'
577591 'tom' => 'and'
578592 'welcome' => undef
579593
580594=begin original
581595
582596The reference is truly dumped and we can finally see what we're dealing with.
583597Our quoting was perfectly valid but wrong for our purposes, with 'and jerry'
584598being treated as 2 separate words rather than a phrase, thus throwing the
585599evenly paired hash structure out of alignment.
586600
587601=end original
588602
589603リファレンスが完全にダンプされて、ついに扱っているものが見えました。
590604クォートは完全に有効でしたが、今回の目的には間違ったものでした;
591605'and jerry' が熟語ではなく、2 つの別々の単語として扱われています;
592606従って、2 つ組のハッシュ構造のアライメントがずれたのです。
593607
594608=begin original
595609
596610The 'B<-w>' switch would have told us about this, had we used it at the start,
597611and saved us a lot of trouble:
598612
599613=end original
600614
601615'B<-w>' オプションはこれについて教えてくれるので、最初に使っておけば、
602616多くの問題から救ってくれていました:
603617
604618 > perl -w data
605619 Odd number of elements in hash assignment at ./data line 5.
606620
607621=begin original
608622
609623We fix our quoting: 'tom' => q(and jerry), and run it again, this time we get
610624our expected output:
611625
612626=end original
613627
614628クォートを修正します: 'tom' => q(and jerry)、そして再実行すると、今度は
615629予想通りの出力が得られます:
616630
617631 > perl -w data
618632 Hello World
619633
620
621634=begin original
622635
623636While we're here, take a closer look at the 'B<x>' command, it's really useful
624637and will merrily dump out nested references, complete objects, partial objects
625638- just about whatever you throw at it:
626639
627640=end original
628641
629642ここにいる間に 'B<x>' コマンドをより近くで見てみると、これは本当に有用で、
630643ネストしたリファレンス、完全なオブジェクト、オブジェクトの一部 - コマンドに
631644与えたものは何でも - 楽しくダンプします:
632645
633646=begin original
634647
635648Let's make a quick object and x-plode it, first we'll start the debugger:
636649it wants some form of input from STDIN, so we give it something non-committal,
637650a zero:
638651
639652=end original
640653
641654簡単なオブジェクトを作って、見てみましょう; まずデバッガを起動します:
642655これは STDIN から何らかの形の入力を要求するので、何か無害なもの - ゼロ - を
643656入力します:
644657
645 > perl -de 0
658 > perl -de 0
646 Default die handler restored.
659 Default die handler restored.
647660
648 Loading DB routines from perl5db.pl version 1.07
661 Loading DB routines from perl5db.pl version 1.07
649 Editor support available.
662 Editor support available.
650663
651 Enter h or `h h' for help, or `man perldebug' for more help.
664 Enter h or `h h' for help, or `man perldebug' for more help.
652665
653 main::(-e:1): 0
666 main::(-e:1): 0
654667
655668=begin original
656669
657670Now build an on-the-fly object over a couple of lines (note the backslash):
658671
659672=end original
660673
661674ここで、複数行を使ってその場でオブジェクトを構築します
662675(バックスラッシュに注意してください):
663676
664 DB<1> $obj = bless({'unique_id'=>'123', 'attr'=> \
677 DB<1> $obj = bless({'unique_id'=>'123', 'attr'=> \
665 cont: {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
678 cont: {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
666679
667680=begin original
668681
669682And let's have a look at it:
670683
671684=end original
672685
673686そして見てみましょう:
674687
675688 DB<2> x $obj
676 0 MY_class=HASH(0x828ad98)
689 0 MY_class=HASH(0x828ad98)
677690 'attr' => HASH(0x828ad68)
678691 'col' => 'black'
679692 'things' => ARRAY(0x828abb8)
680693 0 'this'
681694 1 'that'
682695 2 'etc'
683696 'unique_id' => 123
684697 DB<3>
685698
686699=begin original
687700
688701Useful, huh? You can eval nearly anything in there, and experiment with bits
689702of code or regexes until the cows come home:
690703
691704=end original
692705
693706便利でしょう?
694707ここでほとんどなんでも eval できて、ちょっとしたコードや正規表現を
695708いつまでも実験できます。
696709
697 DB<3> @data = qw(this that the other atheism leather theory scythe)
710 DB<3> @data = qw(this that the other atheism leather theory scythe)
698711
699 DB<4> p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
712 DB<4> p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
700 atheism
713 atheism
701 leather
714 leather
702 other
715 other
703 scythe
716 scythe
704 the
717 the
705 theory
718 theory
706 saw -> 6
719 saw -> 6
707720
708721=begin original
709722
710723If you want to see the command History, type an 'B<H>':
711724
712725=end original
713726
714727コマンド履歴を見たいなら、'B<H>' をタイプします:
715728
716 DB<5> H
729 DB<5> H
717 4: p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
730 4: p 'saw -> '.($cnt += map { print "\t:\t$_\n" } grep(/the/, sort @data))
718 3: @data = qw(this that the other atheism leather theory scythe)
731 3: @data = qw(this that the other atheism leather theory scythe)
719 2: x $obj
732 2: x $obj
720 1: $obj = bless({'unique_id'=>'123', 'attr'=>
733 1: $obj = bless({'unique_id'=>'123', 'attr'=>
721 {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
734 {'col' => 'black', 'things' => [qw(this that etc)]}}, 'MY_class')
722 DB<5>
735 DB<5>
723736
724737=begin original
725738
726739And if you want to repeat any previous command, use the exclamation: 'B<!>':
727740
728741=end original
729742
730743以前に使ったコマンドを繰り返したい場合は、感嘆符を使います: 'B<!>':
731744
732 DB<5> !4
745 DB<5> !4
733 p 'saw -> '.($cnt += map { print "$_\n" } grep(/the/, sort @data))
746 p 'saw -> '.($cnt += map { print "$_\n" } grep(/the/, sort @data))
734 atheism
747 atheism
735 leather
748 leather
736 other
749 other
737 scythe
750 scythe
738 the
751 the
739 theory
752 theory
740 saw -> 12
753 saw -> 12
741754
742755=begin original
743756
744757For more on references see L<perlref> and L<perlreftut>
745758
746759=end original
747760
748761リファレンスについてのさらなる情報については L<perlref> と L<perlreftut> を
749762参照してください。
750763
751764=head1 Stepping through code
752765
753766(コードをステップ実行する)
754767
755768=begin original
756769
757770Here's a simple program which converts between Celsius and Fahrenheit, it too
758771has a problem:
759772
760773=end original
761774
762775以下は摂氏と華氏とを変換する単純なプログラムで、やはり問題を抱えています:
763776
764 #!/usr/bin/perl -w
777 #!/usr/bin/perl
765 use strict;
778 use v5.36;
766779
767 my $arg = $ARGV[0] || '-c20';
780 my $arg = $ARGV[0] || '-c20';
768781
769 if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) {
782 if ($arg =~ /^\-(c|f)((\-|\+)*\d+(\.\d+)*)$/) {
770 my ($deg, $num) = ($1, $2);
783 my ($deg, $num) = ($1, $2);
771 my ($in, $out) = ($num, $num);
784 my ($in, $out) = ($num, $num);
772 if ($deg eq 'c') {
785 if ($deg eq 'c') {
773 $deg = 'f';
786 $deg = 'f';
774 $out = &c2f($num);
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";
782788 } else {
783 print "Usage: $0 -[c|f] num\n";
789 $deg = 'c';
790 $out = &f2c($num);
784791 }
785 exit;
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;
786799
787 sub f2c {
800 sub f2c {
788 my $f = shift;
801 my $f = shift;
789 my $c = 5 * $f - 32 / 9;
802 my $c = 5 * $f - 32 / 9;
790 return $c;
803 return $c;
791 }
804 }
792805
793 sub c2f {
806 sub c2f {
794 my $c = shift;
807 my $c = shift;
795 my $f = 9 * $c / 5 + 32;
808 my $f = 9 * $c / 5 + 32;
796 return $f;
809 return $f;
797 }
810 }
798811
799
800812=begin original
801813
802814For some reason, the Fahrenheit to Celsius conversion fails to return the
803815expected output. This is what it does:
804816
805817=end original
806818
807819なぜか、華氏から摂氏への変換は推測される結果を返すのに失敗します。
808820以下はどうなるかです:
809821
810 > temp -c0.72
822 > temp -c0.72
811 33.30 f
823 33.30 f
812824
813 > temp -f33.3
825 > temp -f33.3
814 162.94 c
826 162.94 c
815827
816828=begin original
817829
818830Not very consistent! We'll set a breakpoint in the code manually and run it
819831under the debugger to see what's going on. A breakpoint is a flag, to which
820832the debugger will run without interruption, when it reaches the breakpoint, it
821833will stop execution and offer a prompt for further interaction. In normal
822834use, these debugger commands are completely ignored, and they are safe - if a
823835little messy, to leave in production code.
824836
825837=end original
826838
827839全く一貫していません!
828840手動でコードにブレークポイントをセットして、何が起きているかを見るために
829841デバッガで実行してみます。
830842ブレークポイントは、デバッガを中断なしで実行するためのフラグで、
831843ブレークポイントに到達すると、実行を停止してさらなる対話のためにプロンプトを
832844出します。
833845通常の使用では、これらのデバッガコマンドは完全に無視され、これらは
834846製品コードに残しても安全です - すこし乱雑かもしれませんが。
835847
836848 my ($in, $out) = ($num, $num);
837849 $DB::single=2; # insert at line 9!
838850 if ($deg eq 'c')
839851 ...
840852
841853 > perl -d temp -f33.3
842854 Default die handler restored.
843855
844856 Loading DB routines from perl5db.pl version 1.07
845857 Editor support available.
846858
847859 Enter h or `h h' for help, or `man perldebug' for more help.
848860
849861 main::(temp:4): my $arg = $ARGV[0] || '-c100';
850862
851863=begin original
852864
853865We'll simply continue down to our pre-set breakpoint with a 'B<c>':
854866
855867=end original
856868
857869'B<c>' をタイプして、単純に予めセットされたブレークポイントまで続けます:
858870
859871 DB<1> c
860872 main::(temp:10): if ($deg eq 'c') {
861873
862874=begin original
863875
864876Followed by a view command to see where we are:
865877
866878=end original
867879
868880引き続いて表示コマンドで今どこにいるかを見ます:
869881
870882 DB<1> v
871883 7: my ($deg, $num) = ($1, $2);
872884 8: my ($in, $out) = ($num, $num);
873885 9: $DB::single=2;
874886 10==> if ($deg eq 'c') {
875887 11: $deg = 'f';
876888 12: $out = &c2f($num);
877889 13 } else {
878890 14: $deg = 'c';
879891 15: $out = &f2c($num);
880892 16 }
881893
882894=begin original
883895
884896And a print to show what values we're currently using:
885897
886898=end original
887899
888900そして今使っている値を表示させます:
889901
890902 DB<1> p $deg, $num
891903 f33.3
892904
893905=begin original
894906
895907We can put another break point on any line beginning with a colon, we'll use
896908line 17 as that's just as we come out of the subroutine, and we'd like to
897909pause there later on:
898910
899911=end original
900912
901913コロンの付いているどの行にも別のブレークポイントを置くことができます;
902914サブルーチンから返ってきたばかりのところである 17 行目を使うことにして、
903915あとからここで一旦停止したいとします:
904916
905917 DB<2> b 17
906918
907919=begin original
908920
909921There's no feedback from this, but you can see what breakpoints are set by
910922using the list 'L' command:
911923
912924=end original
913925
914926これに対する反応はありませんが、一覧 'L' コマンドを使うことで、どの
915927ブレークポイントがセットされているかを見ることができます:
916928
917929 DB<3> L
918930 temp:
919931 17: print "$out $deg\n";
920932 break if (1)
921933
922934=begin original
923935
924Note that to delete a breakpoint you use 'd' or 'D'.
936Note that to delete a breakpoint you use 'B'.
925937
926938=end original
927939
928ブレークポイントを削除するためには 'd' や 'D' を使うことに
940ブレークポイントを削除するためには 'B' を使うことに注意してください。
929注意してください。
930941
931942=begin original
932943
933944Now we'll continue down into our subroutine, this time rather than by line
934945number, we'll use the subroutine name, followed by the now familiar 'v':
935946
936947=end original
937948
938949ここでサブルーチンの中に入っていくことにします; 今回は行番号ではなく、
939950サブルーチン名を使います; その後、今となってはおなじみの 'v' を使います:
940951
941952 DB<3> c f2c
942953 main::f2c(temp:30): my $f = shift;
943954
944955 DB<4> v
945956 24: exit;
946957 25
947958 26 sub f2c {
948959 27==> my $f = shift;
949960 28: my $c = 5 * $f - 32 / 9;
950961 29: return $c;
951962 30 }
952963 31
953964 32 sub c2f {
954965 33: my $c = shift;
955966
956
957967=begin original
958968
959969Note that if there was a subroutine call between us and line 29, and we wanted
960970to B<single-step> through it, we could use the 'B<s>' command, and to step
961971over it we would use 'B<n>' which would execute the sub, but not descend into
962972it for inspection. In this case though, we simply continue down to line 29:
963973
964974=end original
965975
966976ここと 29 行目との間にサブルーチンがあり、そこを B<シングルステップ> したい
967977場合、'B<s>' コマンドも使えますし、サブルーチンは実行するけれども
968978サブルーチン内部は検査しないという 'B<n>' コマンドで
969979ステップオーバーできます。
970980しかし、この場合には、単に 29 行まで進めていきます:
971981
972982 DB<4> c 29
973983 main::f2c(temp:29): return $c;
974984
975985=begin original
976986
977987And have a look at the return value:
978988
979989=end original
980990
981991そして返り値を見てみます:
982992
983993 DB<5> p $c
984994 162.944444444444
985995
986996=begin original
987997
988998This is not the right answer at all, but the sum looks correct. I wonder if
989999it's anything to do with operator precedence? We'll try a couple of other
9901000possibilities with our sum:
9911001
9921002=end original
9931003
9941004これは全く間違った答えですが、合計は正しいように見えます。
9951005演算子の優先順位が何かを行っているのでしょうか?
9961006合計に関してその他の可能性を試してみます:
9971007
9981008 DB<6> p (5 * $f - 32 / 9)
9991009 162.944444444444
10001010
10011011 DB<7> p 5 * $f - (32 / 9)
10021012 162.944444444444
10031013
10041014 DB<8> p (5 * $f) - 32 / 9
10051015 162.944444444444
10061016
10071017 DB<9> p 5 * ($f - 32) / 9
10081018 0.722222222222221
10091019
10101020=begin original
10111021
10121022:-) that's more like it! Ok, now we can set our return variable and we'll
10131023return out of the sub with an 'r':
10141024
10151025=end original
10161026
10171027:-) これはより似ています!
10181028よし、ここで独自の返り値をセットして、'r' を使ってサブルーチンから返ります:
10191029
10201030 DB<10> $c = 5 * ($f - 32) / 9
10211031
10221032 DB<11> r
10231033 scalar context return from main::f2c: 0.722222222222221
10241034
10251035=begin original
10261036
10271037Looks good, let's just continue off the end of the script:
10281038
10291039=end original
10301040
10311041良さそうです; スクリプトの最後まで実行していましょう:
10321042
10331043 DB<12> c
10341044 0.72 c
10351045 Debugged program terminated. Use q to quit or R to restart,
10361046 use O inhibit_exit to avoid stopping after program termination,
10371047 h q, h R or h O to get additional info.
10381048
10391049=begin original
10401050
10411051A quick fix to the offending line (insert the missing parentheses) in the
10421052actual program and we're finished.
10431053
10441054=end original
10451055
10461056実際のプログラムの問題のある行に救急処置(不足していたかっこを挿入する)を
10471057施して、終わりです。
10481058
10491059=head1 Placeholder for a, w, t, T
10501060
10511061(a, w, t, T のためのプレースホルダ)
10521062
10531063=begin original
10541064
10551065Actions, watch variables, stack traces etc.: on the TODO list.
10561066
10571067=end original
10581068
10591069アクション、変数の監視、スタックトレースなど: TODO リストです。
10601070
10611071 a
10621072
10631073 w
10641074
10651075 t
10661076
10671077 T
10681078
1069
10701079=head1 REGULAR EXPRESSIONS
10711080
10721081(正規表現)
10731082
10741083=begin original
10751084
10761085Ever wanted to know what a regex looked like? You'll need perl compiled with
10771086the DEBUGGING flag for this one:
10781087
10791088=end original
10801089
10811090正規表現がどのように見えるか知りたいと思いましたか?
10821091以下のようにするには perl を DEBUGGING フラグ付きでコンパイルする必要が
10831092あります:
10841093
1085 > perl -Dr -e '/^pe(a)*rl$/i'
1094 > perl -Dr -e '/^pe(a)*rl$/i'
1086 Compiling REx `^pe(a)*rl$'
1095 Compiling REx `^pe(a)*rl$'
1087 size 17 first at 2
1096 size 17 first at 2
1088 rarest char
1097 rarest char
1089 at 0
1098 at 0
1090 1: BOL(2)
1099 1: BOL(2)
1091 2: EXACTF <pe>(4)
1100 2: EXACTF <pe>(4)
1092 4: CURLYN[1] {0,32767}(14)
1101 4: CURLYN[1] {0,32767}(14)
1093 6: NOTHING(8)
1102 6: NOTHING(8)
1094 8: EXACTF <a>(0)
1103 8: EXACTF <a>(0)
1095 12: WHILEM(0)
1104 12: WHILEM(0)
1096 13: NOTHING(14)
1105 13: NOTHING(14)
1097 14: EXACTF <rl>(16)
1106 14: EXACTF <rl>(16)
1098 16: EOL(17)
1107 16: EOL(17)
1099 17: END(0)
1108 17: END(0)
1100 floating `'$ at 4..2147483647 (checking floating) stclass `EXACTF <pe>'
1109 floating `'$ at 4..2147483647 (checking floating) stclass
1101anchored(BOL) minlen 4
1110 `EXACTF <pe>' anchored(BOL) minlen 4
1102 Omitting $` $& $' support.
1111 Omitting $` $& $' support.
11031112
1104 EXECUTING...
1113 EXECUTING...
11051114
1106 Freeing REx: `^pe(a)*rl$'
1115 Freeing REx: `^pe(a)*rl$'
11071116
11081117=begin original
11091118
11101119Did you really want to know? :-)
11111120For more gory details on getting regular expressions to work, have a look at
11121121L<perlre>, L<perlretut>, and to decode the mysterious labels (BOL and CURLYN,
11131122etc. above), see L<perldebguts>.
11141123
11151124=end original
11161125
11171126本当に知りたかったですか? :-)
11181127正規表現の動作に関する詳細については、L<perlre>, L<perlretut> を、
11191128(上述の BOL や CURLYN などの)不思議なラベルを解読するには、
11201129L<perldebguts> を参照してください。
11211130
11221131=head1 OUTPUT TIPS
11231132
11241133(出力の小技)
11251134
11261135=begin original
11271136
11281137To get all the output from your error log, and not miss any messages via
11291138helpful operating system buffering, insert a line like this, at the start of
11301139your script:
11311140
11321141=end original
11331142
11341143エラーログからの全ての出力を得て、親切な OS のバッファリングで
11351144メッセージを失わないようにするには、スクリプトの最初に以下のような行を
11361145挿入してください:
11371146
11381147 $|=1;
11391148
11401149=begin original
11411150
11421151To watch the tail of a dynamically growing logfile, (from the command line):
11431152
11441153=end original
11451154
11461155動的に増え続けるログファイルの末尾を監視するには、(コマンドラインから):
11471156
11481157 tail -f $error_log
11491158
11501159=begin original
11511160
11521161Wrapping all die calls in a handler routine can be useful to see how, and from
11531162where, they're being called, L<perlvar> has more information:
11541163
11551164=end original
11561165
11571166全ての die 呼び出しをハンドラルーチンで囲むと、どこで、どのように
11581167呼び出されているかを知るのに有用です; L<perlvar> にさらなる情報があります:
11591168
1160 BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } }
1169 BEGIN { $SIG{__DIE__} = sub { require Carp; Carp::confess(@_) } }
11611170
11621171=begin original
11631172
11641173Various useful techniques for the redirection of STDOUT and STDERR filehandles
11651174are explained in L<perlopentut> and L<perlfaq8>.
11661175
11671176=end original
11681177
11691178STDOUT と STDERR ファイルハンドルのリダイレクトに関する様々な便利な
11701179テクニックが L<perlopentut> と L<perlfaq8> に記述されています。
11711180
11721181=head1 CGI
11731182
11741183=begin original
11751184
11761185Just a quick hint here for all those CGI programmers who can't figure out how
11771186on earth to get past that 'waiting for input' prompt, when running their CGI
11781187script from the command-line, try something like this:
11791188
11801189=end original
11811190
11821191「入力待ち」プロンプトからどうやれば逃れられるのかが分からない全ての
11831192CGI プログラマへの簡単なヒントとして、
11841193CGI をコマンドラインから実行するときに、以下のようなものを試してください:
11851194
11861195 > perl -d my_cgi.pl -nodebug
11871196
11881197=begin original
11891198
11901199Of course L<CGI> and L<perlfaq9> will tell you more.
11911200
11921201=end original
11931202
11941203もちろん L<CGI> と L<perlfaq9> にはもっと多くの情報があります。
11951204
11961205=head1 GUIs
11971206
11981207(GUI)
11991208
12001209=begin original
12011210
12021211The command line interface is tightly integrated with an B<emacs> extension
12031212and there's a B<vi> interface too.
12041213
12051214=end original
12061215
12071216コマンドラインインターフェースは B<emacs> 拡張と密接に統合されていて、
12081217B<vi> インターフェースもあります。
12091218
12101219=begin original
12111220
12121221You don't have to do this all on the command line, though, there are a few GUI
12131222options out there. The nice thing about these is you can wave a mouse over a
12141223variable and a dump of its data will appear in an appropriate window, or in a
12151224popup balloon, no more tiresome typing of 'x $varname' :-)
12161225
12171226=end original
12181227
12191228しかし、これら全てをコマンドラインで実行する必要はありません;
12201229いくつかの GUI の選択肢もあります。
12211230これらのよいところは、マウスカーソルを変数の上に移動させると適切な
12221231ウィンドウやバルーンにそのデータがダンプされ、もう 'x $varname' と
12231232タイプしなくていいことです :-)
12241233
12251234=begin original
12261235
12271236In particular have a hunt around for the following:
12281237
12291238=end original
12301239
12311240特に以下のものの辺りを調べてみてください:
12321241
12331242=begin original
12341243
12351244B<ptkdb> perlTK based wrapper for the built-in debugger
12361245
12371246=end original
12381247
12391248B<ptkdb> ビルドインデバッガのための perlTK ベースのラッパー
12401249
12411250=begin original
12421251
12431252B<ddd> data display debugger
12441253
12451254=end original
12461255
12471256B<ddd> データ表示デバッガ
12481257
12491258=begin original
12501259
12511260B<PerlDevKit> and B<PerlBuilder> are NT specific
12521261
12531262=end original
12541263
12551264B<PerlDevKit> と B<PerlBuilder> は NT 固有です
12561265
12571266=begin original
12581267
12591268NB. (more info on these and others would be appreciated).
12601269
12611270=end original
12621271
12631272注意せよ。
12641273(これらやその他のものに関するさらなる情報を頂ければ幸いです)。
12651274
12661275=head1 SUMMARY
12671276
12681277(まとめ)
12691278
12701279=begin original
12711280
12721281We've seen how to encourage good coding practices with B<use strict> and
12731282B<-w>. We can run the perl debugger B<perl -d scriptname> to inspect your
12741283data from within the perl debugger with the B<p> and B<x> commands. You can
12751284walk through your code, set breakpoints with B<b> and step through that code
12761285with B<s> or B<n>, continue with B<c> and return from a sub with B<r>. Fairly
12771286intuitive stuff when you get down to it.
12781287
12791288=end original
12801289
12811290B<use strict> と B<-w> を使ってどうやって良いコーディングを実践するかを
12821291見ました。
12831292B<perl -d scriptname> とすることで perl デバッガを起動でき、デバッガの
12841293B<p> や B<x> のコマンドでデータを検査できます。
12851294コードの中を通り抜けて、B<b> でブレークポイントを設定し、
12861295B<s> や B<n> でステップ実行を行い、B<c> で再開して、B<r> サブルーチンから
12871296戻ります。
12881297うんざりしたときにはかなり直感的な機能です。
12891298
12901299=begin original
12911300
12921301There is of course lots more to find out about, this has just scratched the
12931302surface. The best way to learn more is to use perldoc to find out more about
12941303the language, to read the on-line help (L<perldebug> is probably the next
12951304place to go), and of course, experiment.
12961305
12971306=end original
12981307
12991308もちろんもっと多くの調べるべきことがありますが、これは表面を
13001309なぞっただけです。
13011310より多くを学ぶための最善の方法は、言語に関してより多くを調べるために
13021311オンラインヘルプを読むために perldoc を使う(おそらく次に進むべき
13031312ところは L<perldebug> でしょう)ことと、もちろん実践です。
13041313
13051314=head1 SEE ALSO
13061315
13071316L<perldebug>,
13081317L<perldebguts>,
1318L<perl5db.pl>,
13091319L<perldiag>,
1310L<dprofpp>,
13111320L<perlrun>
13121321
13131322=head1 AUTHOR
13141323
1315Richard Foley <richard@rfi.net> Copyright (c) 2000
1324Richard Foley <richard.foley@rfi.net> Copyright (c) 2000
13161325
13171326=head1 CONTRIBUTORS
13181327
13191328(貢献者)
13201329
13211330=begin original
13221331
13231332Various people have made helpful suggestions and contributions, in particular:
13241333
13251334=end original
13261335
13271336様々な人々が有益な提案や貢献をしてくれました; 特に:
13281337
13291338Ronald J Kimball <rjk@linguist.dartmouth.edu>
13301339
13311340Hugo van der Sanden <hv@crypt0.demon.co.uk>
13321341
13331342Peter Scott <Peter@PSDT.com>
13341343
13351344=begin meta
13361345
1337Translate: Kentaro Shirakata <argrath@ub32.org> (5.10.0-)
1346Translate: SHIRAKATA Kentaro <argrath@ub32.org> (5.10.0-)
1347Status: completed
13381348
13391349=end meta