Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I see the values of the variables from a Perl trace?

Tags:

perl

My target is to debug (step by step) the sample.pl script below.

The problem: I don’t get the real values of the variables ($top_number , $x , $total).

My question: how to see the real integer values of ($top_number , $x , $total) from the trace output?

What needs to change in perl -d:Trace in order to get numbers, and not: $top_number , $x , $total ?

Example from the trace output:

[root@linux /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?
.
.

[root@linux /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";
like image 684
jon Avatar asked Feb 10 '11 02:02

jon


1 Answers

I assume you want to see the values of the $x and $total variables for each iteration through the loop. There is no indication from the POD for Devel::Trace that it can do that.

However, Devel::DumpTrace can.

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;
like image 86
toolic Avatar answered Oct 15 '22 08:10

toolic