in reply to Re^3: XS/Inline::C concat *any* two SVs.
in thread XS/Inline::C concat *any* two SVs.

The code I posted in the OP now does what I need it to do. Basically the XS equivalent of Perl's

$scalar1 .= $scalar2;

I don't want to create a new scalar, except in the specific instance of a readonly input. The routine will be called many times and I need to accumulate the results in the scalar.

Duplicating the (already appended to), scalar in order to avoid using SvREFCOUNT_inc() is extremely wasteful and completely negates the benefits of perl's dynamically allocated string management. You have a scalar with n bytes, you call sv_catsv() to add m bytes to it. Having done that, you make a complete copy of m+n bytes in order to avoid the unreferenced scalar warning? This makes no sense to me, especially as the scalar of n bytes originates from Perl and has never gone out of scope.

My question still remains, why am I getting that warning?

The question is are there any other things that I need to test for and handle in order that I can handle any pair of SVs as input.

And if I comment out the sv_catsv() call ...

That is the (other) problem. If you call an SV that is undef as the first parameter to sv_catsv(), (which it usually will be on the first call for a particular SV), you get the warning. Hence the need for

if( !SvPOK( a ) ) // Still nothing, must be undef? sv_setpv( a, "" ); // Make it the null string to stop (one pos +sible) // Use of uninitialized value in subroutine entry from sv_cats +v

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.

Replies are listed 'Best First'.
Re^5: XS/Inline::C concat *any* two SVs.
by creamygoodness (Curate) on May 28, 2006 at 14:44 UTC
    You can't concatenate onto a readonly scalar, in either Perl or XS.
    #!/usr/bin/perl use strict; use warnings; use Inline C => <<'END_C'; void readonly(SV *sv) { SvREADONLY_on(sv); } END_C my $foo = "foo"; readonly($foo); # This dies with: # "Modification of a read-only value attempted at cat_readonly.plx lin +e 16." $foo .= "bar"; # unreachable: print "$foo\n";

    So, if you have to deal with the possibility that your first scalar might be readonly, you have to do $a = $a . $b or its equivalent in XS rather than $a .= $b. It might be wasteful, but it's the only way.

    --
    Marvin Humphrey
    Rectangular Research ― http://www.rectangular.com

      If you look at the OP, you'll see that is exactly what I do.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

      If you look at the OP, you'll see that is exactly what I do. But only for readonly scalars.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        Your original post is pretty messed up, so please forgive me if I used your more recent explanation, however imprecise, as a reference instead. I'm trying to help, here.

        --
        Marvin Humphrey
        Rectangular Research ― http://www.rectangular.com