package Tie::Scalar::Logged; use IO::File; require Tie::Scalar; @ISA = qw/Tie::StdScalar/; use strict; use warnings; # TIESCALAR: Mostly self-explanatory, but just to be # thorough, $logfile is the filename of the output file. # $name is a single-quoted string naming the variable that # has been tied. This is for reporting purposes only. # $events is an optional array ref that should contain # the names of the methods you would like to track. Method # names will be case insensitive. They may include: # ALL, TIESCALAR, STORE, FETCH, DESTROY. sub TIESCALAR { my ( $class, $logfile, $name, $events ) = @_; my $self = {}; $self->{Name} = $name; $self->{LOG} = new IO::File ">> $logfile" or return undef; $self->{Value} = undef; if ( not defined ( $events ) or grep {/ALL/i} @$events ) { @$events = qw/TIESCALAR STORE FETCH DESTROY/; } $self->{uc($_)}=1 foreach @$events; print {$self->{LOG}} "$self->{Name} => TIESCALAR\tLogging to $logfile\n" if $self->{TIESCALAR}; bless $self, $class; } sub STORE { my ( $self, $value ) = @_; $self->{Value} = $value; print {$self->{LOG}} "$self->{Name} => STORE\t\tValue = $value\n" if $self->{STORE}; return $self->{Value}; } sub FETCH { my $self = shift; print {$self->{LOG}} "$self->{Name} => FETCH\t\tValue = $self->{Value}\n" if $self->{FETCH}; return $self->{Value}; } sub DESTROY { my $self = shift; print {$self->{LOG}} "$self->{Name} => DESTROY\t\tValue = $self->{Value}\n" if $self->{DESTROY}; $self->{LOG}->close; } 1; # ---------- Begin main ---------- package main; use strict; use warnings; # Simple test: Create a lexical variable and perform a few # operations on it. The logfile will then contain a log of # all activity affecting the variable. my $var; # When tieing a variable to Tie::Scalar::Logged, the parameter list # is as follows: Variable to tie, Package name, Logfile name, # Single-quoted string naming variable that is being tied, and # finally, an optional reference to an array containing one or # more of the following activities that can be logged: # ALL, TIESCALAR, FETCH, STORE, DESTROY. (Case/Order insensitive.) tie $var, "Tie::Scalar::Logged", 'log.txt', '$var', [qw/all/]; $var = 10; $var += 10; print $var, "\n";