On Thu, 01 May 2008, Erland Sommarskog wrote:
> 
> I have in my XS module a routine that converts the charcaters of an SV
> from one code page to another. The conversion is done "in-place". That is,
> I retrieve the text pointer, and then I rewrite the area pointed to. For
> reference, the full code for the routine is included at the end of this
> post.

In general, you should never change SVs that are marked as read-only.

    if (SvREADONLY(sv))
        croak("Cannot modify READONLY variable");

I'm not quite sure if it is worthwhile to also check for SvLEN()
being 0 before attempting to modify the SV, but I would think all
SVs that are SvPOK() and have SvLEN() being 0 should also have
the SvREADONLY() bit set. (SvLEN being zero means that the PV
is managed externally and not (necessarily) even allocated by
the Perl memory manager).

In particular, you cannot update hash keys in-place at all: besides
them sharing memory in a shared hash key table, you would also end
up with a different hash for the new key, so you would be breaking
hash lookup.  If you want to change the hash key, you need to remove
the entry using the old key and add it back using the new key.

Cheers,
-Jan



Reply via email to