Hatena::ブログ(Diary)

Perl Tutorial by Code Examples

2010-03-02

Debugger Manual

Perl one of useful tools is debugger. If you use debugger, you can execute line one by one and see the value in variable, and set break point and stop the position.

Debugger is very useful, but I think many Perl user don't know debugger well. If you know debugger, you don't do programming without debugger. I want you to use debugger more. I summary debugger command and technique.

Script for debug

I prepare script for debug. Please save the name debug.pl. I describe debugger by using this script.

my $message = 'Hello';
my @nums = (1, 2, 3);
my %scores = (math => 80, english => 77);

my $twice = twice(5);

#  break  point 
$DB::single = 1;

for my $num (@nums) {
    
  #  condition   break  point 
  if ($num == 2) { $DB::single = 1 }
  print "$num\n";
}

sub twice {
  my $num = shift;
  
  return $num * 2;
}

Start debugger

Run Perl by -d option to start debugger.

perl -d debug.pl

Debugger start, and stop at the first line.

main::(a.pl:4): my $message = 'Hello';

use "q" to end debugger.

q

Use "h" to see help.

h

Let's learn debugger command in order.

1. Executing command

I describe Perl debugger executing command.

n - 1 line execution

Use n to execute one line of script. Input "n" and put "Enter", forward to next line.

At first state, you execute "n", the following is displayed. You can confirm one line forward.

main::(a.pl:5): my @nums = (1, 2, 3);

Contine "n", "Enter", "n", Script go to the end, and script is finished.

Debugged program terminated.  Use q to quit or R to restart,

You are asked that you stop debug or restart debug, but if you do debug again, you end debug by q.

"n", "Enter", "n", "Enter" is a little boring. You can also forward lines by "n", "Enter", "Enter", "Enter". If "n" or "s" is input just before, you can continue before command by "Enter".

n is maybe n of next.

s - single step execution

Use s to do single step execution. Difference of n is that s can see subroutine internal process.

Do "n" and forward script to the call of subroutine.

main::(a.pl:8): my $twice = twice(5);

At this line you input "s", you can move into the internal of subroutine.

main::twice(a.pl:21):       my $num = shift;
c - execute script until break point or specified line

"c" is command to execute script until break point or specified line. c is maybe c of continue.

c 5
main::(a.pl:5): my @nums = (1, 2, 3);

Next, if you input c without argument, script execute until next break point.

c
main::(a.pl:13):        for my $num (@nums) {
# Break point 
$DB::single = 1;

This is break point which can embed into script. If you want to see the content of variable, if you set break point just before it, you can stop script at that position. This is very useful.

2. Command for display

I describe command for display.

p - Display variable content

You can display variable content by "p". For example, you can do the following way to display content of variable $message.

p $message
x - Display variable content recursively

"p" is useful to display scalar variable content. It is not suitable to display hash, array, and complex data structure.

By "x" you can display data internal structure visible. Note that you must pass reference as x argument.

For example, you do the following to display content of %scores. \ is to get reference.

x \%scores

As the following, data of variable is displayed visible.

0  HASH(0x19b205c)
   'english' => 77
   'math' => 80

3. Other often used command

I describe other often used command.

v - display lines around

You can display line around by "v".

v

If you input "v" at the forth line, you can see the lines around.

1:      use strict;
2:      use warnings;
3
4==>    my $message = 'Hello';
5:      my @nums = (1, 2, 3);
6:      my %scores = (math => 80, english => 77);

You can also bellow lines if you contine to input "v".

. - Display current line

If you continue to input "v", display go down to bellow lines. In that case, you can use "." to return back current line.

.
Perl statement

You can execute whatever Perl statement in debugger. The following is example that check variable is defined.

print defined $message;

It is useful to check because you can execute whatever statement.

4. Debug technique

I introduce often used debug technique.

Break point in source code

In Perl, you can embed break point into source code. If you assign DB class(class for debugger) single variable to 1, at that position debugger switch single step process.

$DB::single = 1;

This is really useful. Let's use it many times. Don't forget to remove it from script after you finish test, .

Break point with condition

When you use debugger, you will think you want to stop debugger in only some condition and see content of varibale. In that case, you can use combination of condition statement and "$DB::single = 1". In this example, if $num is 2, debugger stop at that position.

if ($num == 2) { $DB::single = 1 }
Search position where warning occur

When you test script, you will often see the warnings that undefined value is used.

Use of uninitialized value

Especially it is difficult to find the reason when the warnings occur in for and while loop. This mean that a value isn't assigned to the variable which must be assigned. you should search the position that warnings occur.

Most people forward debugger at the point that you think error maybe occur, but we have the way to catch warnings.

In Perl, warning is caught as signal, so you can set the following break point in loop block.

# Set break point to catch warning
$SIG{__WARN__} = sub {
  $DB::single = 1;
};

Use debugger more

I finish the description of debugger command and technique. There are many other debug commands, but if you master the commands I describe in this entry, you maybe don't have trouble at debug. Let's use debugger more because debugger is very useful tool.


Table of contents

スパム対策のためのダミーです。もし見えても何も入力しないでください
ゲスト


画像認証

トラックバック - http://d.hatena.ne.jp/perlcodesampleen/20100302/1269670120