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


in reply to Reliability of SvREADONLY

I am not quite sure why it is said above that type coercion is going to cause a problem here. Let me provide a little example.
use ExtUtils::testlib; use Flag; #all local, sorry my $c = 'test'; Flag::sv_set_flag($c, 'READONLY'); #$c++; #$c = 'not a test'; $c = 5;
Any of those examples produce a "Modification of a read-only value" error. Naturally. And as long as that works, that we cannot directly modify the value, we're set. Certain types of behavior can modify the underlying data structure, but not to the point of changing the eventual data.
use Devel::Peek; use ExtUtils::testlib; use Flag; my $c = 15; Flag::sv_set_flag($c, 'READONLY'); print Dump $c; print "$c"; print Dump $c;
Naturally the interpolation causes the IV to become an PVIV. But our PV value will be "15"\0. So in any circumstance our behavior is normal. Yes the structure has changed, but from a user standpoint our data is readonly.

Is there a specific need to protect the data from perl internals that could possibly be ignoring the readonly state of a variable? If not, then type coercion isn't a problem at all.

Replies are listed 'Best First'.
Re^2: Reliability of SvREADONLY
by ikegami (Patriarch) on Dec 14, 2006 at 04:14 UTC

    Naturally the interpolation causes the IV to become an PVIV.

    That causes a write to the memory page, causing it to become unshared, ...

    I am not quite sure why it is said above that type coercion is going to cause a problem here.

    ...thus type coercion causes a problem.