Warning, a curious bug in 5.6.1 with tied variables and
interpolation/concatenation, may cause some mental anguish. It seems
that a double FETCH is happening, but only when the variable is
interpolated at the front of a string that contains trailing
characters (or the equivalent, when the variable is the leading term
in a concatenation operation):
#!/usr/bin/perl -w
use strict;
{
package Tie::IncScalar;
sub TIESCALAR {
my $class = shift;
my $self = shift || 0;
return bless \$self, $class;
}
sub FETCH {
my $self = shift;
$$self++;
}
sub STORE {
my $self = shift;
$$self = shift;
}
}
tie my $x, 'Tie::IncScalar';
$x = 0;
print "Case: 1 (good)\n";
print $x for 1..10;
print "\n";
$x = 0;
print "$x" for 1..10;
print "\n\n";
$x = 0;
print "Case: 2 (good)\n";
print " $x" for 1..10;
print "\n";
$x = 0;
print " " . $x for 1..10;
print "\n\n";
$x = 0;
print "Case: 3 (good)\n";
print " $x " for 1..10;
print "\n";
$x = 0;
print " " . $x . " " for 1..10;
print "\n\n";
$x = 0;
print "Case: 4 (yikes)\n";
print "$x " for 1..10;
print "\n";
$x = 0;
print $x . " " for 1..10;
print "\n\n";
__END__
This produces the following output with 5.6.1:
Case: 1 (good)
0123456789
0123456789
Case: 2 (good)
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Case: 3 (good)
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
0 1 2 3 4 5 6 7 8 9
Case: 4 (yikes)
1 3 5 7 9 11 13 15 17 19
1 3 5 7 9 11 13 15 17 19
Case 4 works fine with 5.00503 and 5.6.0. (I noticed this behaviour
when playing with Tie::Cycle earlier ... same thing, skips elements
in the cycle ... and if you first encounter it while playing with a
two value (boolean) cycle, you may feel like you are simply whacked
out because you can't see why the cycle isn't cycling :-(
I searched p5p and it appears to have been reported and patched in
development versions --- I just wanted to let people here know so
they might avoid some confusion if they run into it. A relevant p5p
link (from which the patch is linked) is:
here