=head1 名前 perldebug - Perl のデバッグ =head1 説明 まず初めに. B<-w> スイッチは試しましたか? もしあなたが Perl デバッガ初心者でしたら, L を 読んでみた方がよいでしょう. それはデバッガを使うためのチュートリアルです. =head1 Perl デバッガ Perl を B<-d> スイッチ付きで呼び出すと, スクリプトを Perl ソースデバッガ の中で起動することができます. これはインタラクティブな Perl 環境として 動作します. この中では, ソースコードの調査, ブレークポイントの設定, スタックトレースの取得, 変数の値の変更などを行えるデバッガコマンドを 待機します. この環境はとても便利なので, Perl が何を行っているのかを インタラクティブに見るためにデバッガを起動するでしょう. 例えば: $ perl -d -e 42 Perl において, デバッガはよくあるような特別にコンパイルされた環境とする 分離したプログラムという形を取っていません. 代わりに, B<-d> フラグが コンパイラに対して解析木のにソースの情報を入れておくようにコンパイラに 通知しています. これは, デバッガ上で動かすためにはデバッグしようとする コードはまず正常にコンパイルされなければならないことを意味します. そしてインタプリタが起動するとデバッガを含んでいる特別な Perl ライブラリが プリロードされます. プログラムは最初にランタイムに実行される文の前で停止します(コンパイルタイム の文に関しては後述します). そしてデバッガコマンドの入力待ちになります. 一般的な予想とは違って, デバッガが停止してコード行を表示したときは いつでも, 実行しようとI<している>行を表示します. 実行した行ではありません. デバッガが認識できない全てのコマンドは Perl コードとしてカレントパッケージ で実行(C)されます. (デバッガ自身の情報はDBパッケージに記録されます.) このCは暗黙のスコープを持つことに注意してください. 結果として, 新しく導入されたレキシカル変数や変更されたキャプチャ バッファの内容は, eval の後破棄されます. デバッガは, Perl の学習 にとてもよい環境です. しかし1つのスコープ内に置く必要のある構成の 実験をしたいときにはそれらは1行に書く必要があります. デバッガのプロンプトに入力されたテキストの, 先頭及び末尾の空白は それを処理する前に取り除かれます. デバッガコマンドがプログラム内の関数に 一致したときは, デバッガコマンドぽく無ければ, 例えば C<;> で終わっている, C<+> がある, 括弧でくるまれているなら, 単に関数が優先されます. (訳注:優先されないみたいだから訳せてないぽい..) =head2 デバッガコマンド デバッガは以下のコマンドを認識します: =over 12 =item h ヘルプメッセージの要約を表示します. =item h [command] 与えられたデバッガコマンドのヘルプメッセージを表示します. =item h h Cの特別な引数は完全なヘルプページを表示します. これはとても 長いです. Cコマンドの(若しくはほかのコマンドでも)出力が画面外まででてしまうときは, コマンドをパイプ記号で始めることでページャに通すことができます. DB> |h h ページャは, C コマンドで変更できます. =item p expr Same as C in the current package. In particular, because this is just Perl's own C function, this means that nested data structures and objects are not dumped, unlike with the C command. The C filehandle is opened to F, regardless of where STDOUT may be redirected to. =item x [maxdepth] expr Evaluates its expression in list context and dumps out the result in a pretty-printed fashion. Nested data structures are printed out recursively, unlike the real C function in Perl. When dumping hashes, you'll probably prefer 'x \%h' rather than 'x %h'. See L if you'd like to do this yourself. The output format is governed by multiple options described under L. If the C is included, it must be a numeral I; the value is dumped only I levels deep, as if the C option had been temporarily set to I. =item V [pkg [vars]] Display all (or some) variables in package (defaulting to C
) using a data pretty-printer (hashes show their keys and values so you see what's what, control characters are made printable, etc.). Make sure you don't put the type specifier (like C<$>) there, just the symbol names, like this: V DB filename line Use C<~pattern> and C for positive and negative regexes. This is similar to calling the C command on each applicable var. =item X [vars] Same as C. =item y [level [vars]] Display all (or some) lexical variables (mnemonic: C variables) in the current scope or I scopes higher. You can limit the variables that you see with I which works exactly as it does for the C and C commands. Requires the C module version 0.08 or higher; will warn if this isn't installed. Output is pretty-printed in the same style as for C and the format is controlled by the same options. =item T Produce a stack backtrace. See below for details on its output. =item s [expr] Single step. Executes until the beginning of another statement, descending into subroutine calls. If an expression is supplied that includes function calls, it too will be single-stepped. =item n [expr] Next. Executes over subroutine calls, until the beginning of the next statement. If an expression is supplied that includes function calls, those functions will be executed with stops before each statement. =item r Continue until the return from the current subroutine. Dump the return value if the C option is set (default). =item Repeat last C or C command. =item c [line|sub] Continue, optionally inserting a one-time-only breakpoint at the specified line or subroutine. =item l List next window of lines. =item l min+incr List C lines starting at C. =item l min-max List lines C through C. C is synonymous to C<->. =item l line List a single line. =item l subname List first window of lines from subroutine. I may be a variable that contains a code reference. =item - List previous window of lines. =item v [line] View a few lines of code around the current line. =item . Return the internal debugger pointer to the line last executed, and print out that line. =item f filename Switch to viewing a different file or C statement. If I is not a full pathname found in the values of %INC, it is considered a regex. Ced strings (when accessible) are considered to be filenames: C and C access the body of the 7th Ced string (in the order of execution). The bodies of the currently executed C and of Ced strings that define subroutines are saved and thus accessible. =item /pattern/ Search forwards for pattern (a Perl regex); final / is optional. The search is case-insensitive by default. =item ?pattern? Search backwards for pattern; final ? is optional. The search is case-insensitive by default. =item L [abw] List (default all) actions, breakpoints and watch expressions =item S [[!]regex] List subroutine names [not] matching the regex. =item t Toggle trace mode (see also the C option). =item t expr Trace through execution of C. See L for examples. =item b Sets breakpoint on current line =item b [line] [condition] Set a breakpoint before the given line. If a condition is specified, it's evaluated each time the statement is reached: a breakpoint is taken only if the condition is true. Breakpoints may only be set on lines that begin an executable statement. Conditions don't use C: b 237 $x > 30 b 237 ++$count237 < 11 b 33 /pattern/i =item b subname [condition] Set a breakpoint before the first line of the named subroutine. I may be a variable containing a code reference (in this case I is not supported). =item b postpone subname [condition] Set a breakpoint at first line of subroutine after it is compiled. =item b load filename Set a breakpoint before the first executed line of the I, which should be a full pathname found amongst the %INC values. =item b compile subname Sets a breakpoint before the first statement executed after the specified subroutine is compiled. =item B line Delete a breakpoint from the specified I. =item B * Delete all installed breakpoints. =item a [line] command Set an action to be done before the line is executed. If I is omitted, set an action on the line about to be executed. The sequence of steps taken by the debugger is 1. check for a breakpoint at this line 2. print the line if necessary (tracing) 3. do any actions associated with that line 4. prompt user if at a breakpoint or in single-step 5. evaluate line For example, this will print out $foo every time line 53 is passed: a 53 print "DB FOUND $foo\n" =item A line Delete an action from the specified line. =item A * Delete all installed actions. =item w expr Add a global watch-expression. We hope you know what one of these is, because they're supposed to be obvious. =item W expr Delete watch-expression =item W * Delete all watch-expressions. =item o Display all options =item o booloption ... Set each listed Boolean option to the value C<1>. =item o anyoption? ... Print out the value of one or more options. =item o option=value ... Set the value of one or more options. If the value has internal whitespace, it should be quoted. For example, you could set C to call B with those specific options. You may use either single or double quotes, but if you do, you must escape any embedded instances of same sort of quote you began with, as well as any escaping any escapes that immediately precede that quote but which are not meant to escape the quote itself. In other words, you follow single-quoting rules irrespective of the quote; eg: C or C. For historical reasons, the C<=value> is optional, but defaults to 1 only where it is safe to do so--that is, mostly for Boolean options. It is always better to assign a specific value using C<=>. The C