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

Drawing on the code that has so far been presented, it looks to me that you might want:
SV* test( SV *a, SV *b ) { SV * c; if(SvREADONLY(a)) { c = newSVsv(a); sv_catsv(c, b); return c; } sv_catsv(a,b); c = newSVsv(a); return c; }
I don't know how to avoid the "uninitialized value in subroutine entry" warning (apart from the obvious). Predicting whether you're going to get it or not is a little tricky. Of the 2 following subs, only test5() produces the warning for me:
SV * test4(SV * a, SV * b){return newSVuv(42);} SV * test5(char * a, char * b){return newSVuv(42);}
And if I comment out the sv_catsv() call in the code that vkon posted, then the warning disappears. Obviously the exact nature of what the xsub does has a bearing on whether we get that warning.

Cheers,
Rob

Replies are listed 'Best First'.
Re^4: XS/Inline::C concat *any* two SVs.
by BrowserUk (Patriarch) on May 28, 2006 at 14:15 UTC

    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.
      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.