Alok Bhatt wrote:
Hi All,

 Can someone please tell me how do we keep track of a
variable. ie Whenever a particular variable is
accessed (printed, incremented .... etc.), a counter
should increase, so that we can know how many times it
has been accesssed.

The mechanism is tie, see `perldoc tie`

Here is an example: (See CPAN for others)

#!/usr/bin/perl

package Traced::Var;

use strict;

require Tie::Scalar;

our @ISA = qw(Tie::StdScalar);

sub FETCH {
    my $self = shift;
    print "FETCH: value is " . ${$self} . "\n";
    return ${$self};
}

sub STORE {
    my $self = shift;
    my $val  = shift;
    print "STORE: old value is " . ${$self} . "\n";
    ${$self} = $val;
    print "STORE: new value is " . ${$self} . "\n";
    return ${$self};
}

package main;

my $var;
tie $var, 'Traced::Var';

$var = 42;
print "$var\n";

$var = "The answer";
print "$var\n";

__END__


-- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to