When read-only isn't

DAxelrod on 2006-11-01T20:38:32

Step up, one and all, and see me modify a simple scalar only by reading it. Nothing up my sleeves.

my ($num, $let, $foo);
$num = "z";
$let = $num;
#$foo = 1 + $num; #this does not modify $num
$num++; $let++;
print "num:$num let:$let\n";
__END__
Output:
num:aa let:aa

Output with 1+$num line uncommented:
num:1 let:aa

This means we have a line that makes no assignment to a variable, but still changes that variable.

Critics will call this dangerous and unexpected action at a distance. Proponents will note that a warning is triggered, read-only statements are still an indication of What You Mean, and that this probably makes excellent fodder for obfus.

Admittedly, this is (sorta) documented. Perlop mentions "If, however, the variable has been used in only string contexts since it was set," the ++ operator acts differently.

Abigail's comment to this discussion about side effects inspired this experiment^W magic trick.