<?xml version="1.0" encoding="windows-1252"?>
<node id="910460" title="kimmel's scratchpad" created="2011-06-19 14:12:28" updated="2011-06-19 14:12:28">
<type id="182711">
scratchpad</type>
<author id="910459">
kimmel</author>
<data>
<field name="doctext">
working title: Debugging: Inspecting a variable as the value changes 

&lt;p&gt;This is a simple tutorial on how to find out when a variable's value changes without needing prior knowledge of the codebase.&lt;/p&gt;

It is common practice when writing/debugging/refactoring/messing around with a program that you want to know when a certain variable is set or its value changes. For example I was looking at this piece code
&lt;code&gt;
if ( $show_banner == 1 ) {
    print STDERR "$app_name $version ($app_www)\n";
    print STDERR "$copyright $author, $author_email\n\n";
}
&lt;/code&gt;
and I realized it was never getting executed. I could have just sprinkled in some print $show_banner; statements but this code was written by another programmer so I am not familiar with its structure and flow so I would waste time learning the code to know the best places to put a print statement. 

&lt;p&gt;There has to be a faster way to track this variable and I know this is a pretty common problem. So what is the Perl answer you ask? CPAN. There I found the Tie::Trace module.&lt;/p&gt;

&lt;p&gt;Tie::Trace allows you to watch a variable and every time the value changes you get a debug message. Here is how I added it to my existing program.&lt;/p&gt;
&lt;code&gt;
use Tie::Trace qw( watch );

my $show_banner = 1;
watch $show_banner;
&lt;/code&gt;
Then I ran my application and got the following output. 
&lt;code&gt;
main:: $show_banner =&gt; 0 at ./colordiff.pl line 156.
&lt;/code&gt;
From there I ran the test suite to see if $show_banner changed due to any edge cases. Each test in the test suite produced the same above output.

&lt;p&gt;Now I knew where the value was being changed. From there it was a simple change to an if statement and a rerun of the application test suite to verify no regressions were introduced.&lt;/p&gt;

&lt;p&gt;The watch function provided by Tie::Trace does all the heavy lifting by issuing print Dumper statements on the variable as it changes. This is an invaluable tool for following value changes of variables as a program executes.


</field>
</data>
</node>
