2011-02-10 82 views
5

我的目標是調試(一步一步)下面的sample.pl腳本。如何從Perl跟蹤中查看變量的值?

問題:我沒有得到變量的實際值($ top_number,$ x,$ total)。

我的問題:如何從跟蹤輸出中查看($ top_number,$ x,$ total) 的實數整數值?

需要更改perl -d:Trace以獲取數字,而不是:$ top_number,$ x,$ total?從跟蹤輸出

例子:

[[email protected] /tmp]# perl -d:Trace ./sample.pl 
>> ./sampl.pl:9: $top_number = 100; 
>> ./sampl.pl:10: $x = 1; 
>> ./sampl.pl:11: $total = 0; 
>> ./sampl.pl:12: while ($x <= $top_number) { 
>> ./sampl.pl:13:  $total = $total + $x; # short form: $total += $x; 
>> ./sampl.pl:14:  $x += 1;    # do you follow this short form? 
>> ./sampl.pl:13:  $total = $total + $x; # short form: $total += $x; 
>> ./sampl.pl:14:  $x += 1;    # do you follow this short form? 
>> ./sampl.pl:13:  $total = $total + $x; # short form: $total += $x; 
>> ./sampl.pl:14:  $x += 1;    # do you follow this short form? 
. 
. 

[[email protected] /tmp]#more sample.pl script 

#!/usr/bin/perl 


$top_number = 100; 
$x = 1; 
$total = 0; 
while ($x <= $top_number) { 
    $total = $total + $x; # short form: $total += $x; 
    $x += 1;    # do you follow this short form? 
} 

print "The total from 1 to $top_number is $total\n"; 

回答

7

我假設你想看到的$x$total變量的值,通過循環每次迭代。 POD沒有跡象表明Devel::Trace可以做到這一點。

但是,Devel::DumpTrace可以。

perl -d:DumpTrace ./sample.pl 

>>>>> hw.pl:7:  $top_number:100 = 100; 
>>>>> hw.pl:8:  $x:1 = 1; 
>>>>> hw.pl:9:  $total:0 = 0; 
>>>>> hw.pl:10:  while ($x:1 <= $top_number:100) { 
>>>>> hw.pl:11:   $total:1 = $total:0 + $x:1; # short form: $total:0 += $x:1; 
>>>>> hw.pl:12:   $x:2 += 1;    # do you follow this short form? 
>>>>> hw.pl:11:   $total:3 = $total:1 + $x:2; # short form: $total:1 += $x:2; 
>>>>> hw.pl:12:   $x:3 += 1;    # do you follow this short form? 
>>>>> hw.pl:11:   $total:6 = $total:3 + $x:3; # short form: $total:3 += $x:3; 
+1

`Devel :: DumpTrace`就在幾天前發佈。溫和地報告你發現的任何錯誤。 :-) – mob 2011-02-10 04:06:12

+0

@Mob別擔心我會這麼做 – jon 2011-02-10 04:34:50