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


in reply to Shouldn't LITERAL references be readonly? (updated)

Both the hash constructor ({}) and the array constructor ([]) create a fresh variable and a fresh reference to it.

"TEST", 42 and undef do not create anything; they simply place a previously-constructed scalar on the stack. Changing these scalars would be bad, so they are protected from being changed.

For example, take the following code:

for (1..2) { for ((), 4..6) { say ++$_; } say ""; }

Before 5.20, it output the following:

5 6 7 6 7 8

That is something that isn't read-only but should be. If "TEST", 42 or undef weren't read-only, a similar problem would occur.[1]

There's no such problem with the newly created references from {} and []. {} and [] are akin to "<$x>" (also not read-only), not "TEST".


  1. It would be particularly bad if the scalar returned by undef wasn't read-only. That particular scalar (&PL_sv_undef) is used all over the place. For example, if ++undef didn't fail, it would cause read to start returning true on failure.