On Thu, 01 May 2008, Jan Dubois wrote:
> 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 forgot to add:

Generally, you should not try to modify SVs that were passed in to your
XS code at all; always try to return results in new SVs.

Here is an example that most XS code doesn't support (assignment to magical
SVs):

  sub foo { $_[0] = "foobar" }
  $a = "abc123xyz";
  foo( substr($a,3,3) );
  # print "abcfoobarxyz"
  print $a;

Try implementing foo() in XS and see if it works...

And trust me, if this is an external API, then people will eventually
pass things like lvalue substr() expressions, $1 regexp variables,
tied hash elements etc. to your code.  Treating everything correctly
at the XS level is quite a bit of work, especially if you want to
support older Perl versions too.

Cheers,
-Jan

Reply via email to