Code Comments
Programming Forum and web based access to our favorite programming groups.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. Thanks in Advance, Regards, Alok __________________________________ Do you Yahoo!? Yahoo! Mail - 50x more storage than other providers! http://promotions.yahoo.com/new_mail
Post Follow-up to this messageAlok 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__
Post Follow-up to this message
Show a Printable Version
Email This Page to Someone!
Receive updates to this thread
Powered by vBulletin
Copyright 2000-2006 Jelsoft Enterprises Limited.