http://qs321.pair.com?node_id=11120333


in reply to Re^4: Shouldn't references be readonly?
in thread Shouldn't LITERAL references be readonly? (updated)

I doubt it for the Perl interpreter, but I do know why compilers I've worked with treat numeric and string literals as constants: Optimization.

Constant literals can be packed tightly, like sardines in a box. If you have the string literal "foo" in several places of your program, all of them can point to the same memory location. This would break, of course, if aliasing (or call-by-reference in subroutines) were able to change the values.

References, on the other hand, can never be optimized like this: Two anonymous instances of [] need to be able to lead different lives because you might want to push different values into them.

So there is a reason why a compiler (or interpreter) might want to treat string and number literals as constants, a reason which does not exist for anonymous references.

BTW: Perl's constant handling is only skin-deep:

$ perl -MData::Dump=dump -E 'dump map { $_ = 'boo' } ( 0 )' Modification of a read-only value attempted at -e line 1. $ perl -MData::Dump=dump -E 'dump map { $_ = 'boo' } ( 0+0 )' "boo"