Damian Conway's book OO Perl, in discussing ties, suggests that the
following will create a scalar that increments by one everytime its value is
accessed:
package Incr;
sub TIESCALAR
{
my ($class) = @_;
my $value = 0;
bless \$value, $class;
}
sub FETCH
{
my ($value) = @_;
return ++${$value};
}
But, when I do the following:
#!perl
use lib "C:\\Perl";
use Incr;
tie $foo, "Incr";
print "$foo\n";
print "$foo\n";
print "$foo\n";
print "$foo\n";
print "$foo\n";
exit;
I get:
2
4
6
8
10
Presumable because FETCH calls FETCH itself to do the incrementing,
resulting in a double increment! How can one get a single increment with a
tie?