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".
- 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.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Shouldn't references be readonly? (updated)
by LanX (Saint) on Aug 06, 2020 at 12:08 UTC | |
by ikegami (Patriarch) on Aug 07, 2020 at 01:03 UTC | |
by ikegami (Patriarch) on Aug 07, 2020 at 01:19 UTC | |
by LanX (Saint) on Aug 07, 2020 at 10:34 UTC | |
by ikegami (Patriarch) on Aug 07, 2020 at 19:54 UTC | |
|
In Section
Seekers of Perl Wisdom