Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

Keeping track of a variable
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

Report this thread to moderator Post Follow-up to this message
Old Post
Alok Bhatt
07-29-04 08:55 AM


Re: Keeping track of a variable
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__


Report this thread to moderator Post Follow-up to this message
Old Post
Randy W. Sims
07-29-04 01:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 04:31 PM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.