in reply to substitution not recompile

It doesn't work because you removed the 'x' the first time around.

$val = 1; while ($val < 5) { $foo = "some sentence x"; # Move this here $foo =~ s/x/$val/; print "$val $foo\n"; $val++; }

or

$foo = "some sentence x"; $val = 1; while ($val < 5) { my $foo = $foo; # Add this. $foo =~ s/x/$val/; print "$val $foo\n"; $val++; }