use strict; use warnings; package Trackit; my $varname; sub TIESCALAR { my $class = shift; $varname = shift; bless \my($self), $class; } sub STORE { ${ $_[0] } = $_[1]; document ( "STORE", $_[1] ); return ${ $_[0] }; } sub FETCH { my $self = shift; document( "FETCH", ${$self} ); return ${ $self } } sub DESTROY { my $self = shift; document ( "DESTROY", ${$self} ); } sub document { my ( $action, $value ) = @_; my $fh; open $fh, ">>log.txt" or die $!; print $fh "$varname => Action: $action, Value = $value\n"; close $fh; } # Begin main ---------------------------- package main; my $var; tie $var, "Trackit", '$var'; # Test our handywork... $var = 10; $var = $var + 10; open FH, "<log.txt" or die $!; while ( <FH> ) { print $_; } close FH;
Ok, here are the areas I see a need for improvement. First, it seems kludgy to have to pass as a string the name of the variable we're working with so that it can be printed to the logfile. Next, I haven't provided a way to user-specify the name of the logfile, but that's an easy fix. ...anything else?
Dave
In reply to Re: Debugging with tied variables
by davido
in thread Debugging with tied variables
by ysth
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |