Date: Mon, 21 Nov 2005 11:36:54 -0800 From: Dave Mitchell via RT Subject: Re: [perl #37722] interpolated tie()d variable weirdness On Mon, Nov 21, 2005 at 03:21:13AM -0800, Michele Dondi wrote: > I noticed a strange and appearently inconsistent behaviour (to the best of > my knowledge non documented) when a tie()d variable is put at the *beginning* > of an iterpolating string. Minimal example code follows: > > > #!/usr/bin/perl -l > > use strict; > use warnings; > > sub TIESCALAR { bless \my $i, shift } > sub FETCH { ${shift,}++ } > > tie my $s, 'main'; > print "$s$s$s"; > print "$s$s$s"; > > tie my $t, 'main'; > print " $t$t$t"; > print " $t$t$t"; > > __END__ > > > Output: > > > 112 > 445 > 012 > 345 > > Thanks for the report, but this isn't actually a bug! Similar sorts of effects can be seen without tie, eg $ perl -le 'print ++$x . ++$x . ++$x' 223 $ perl -le 'print " " . ++$x . ++$x . ++$x' 123 $ Note that interpolated strings are just short hand for string concats, and in fact that's how perl handles them internally, ie " $s$s$s" is equivalent to (("".$s).$s).$s and in general when you when you start using the same variable more than once in an expression, where use of that variable has side effect, unexpected things will generally happen. -- Never do today what you can put off till tomorrow.