Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, monks!
I want to write on XS something like this:
sub testSub { my $test_var = shift; $$test_var = "Hello!;)\n"; return 1; }

I'v tried to use following way:
int testSub(const char** test_var) CODE: ...
But it does not works =\

Replies are listed 'Best First'.
Re: xs and references
by chromatic (Archbishop) on Feb 27, 2009 at 17:53 UTC

    It doesn't work because Perl is not C. Scalars in Perl are SVs (and Perl strings are PVs, not chars). Also Perl passes and returns values on a stack (not the C stack), not through C's calling conventions.

    Read perlxstut to understand how passing and receiving parameters work. Then browse perlapi for specific functions. I believe (without reading it myself this morning) you need to check that you have a valid RV (something like SvRVOK) -- which represents a reference in Perl -- then access the referent and assign its PV (potentially upgrading it to contain a PV).

Re: xs and references
by syphilis (Archbishop) on Feb 28, 2009 at 06:53 UTC
    As a starter:
    use warnings; use Inline C => Config => CLEAN_AFTER_BUILD => 0, BUILD_NOISY => 1; use Inline C => <<'EOC'; int testSub(SV * x) { if(!SvROK(x)) croak ("Not a reference"); sv_setpv(SvRV(x), "Hello!;)\n\0"); return 1; } EOC $init = "barbarella"; $test_var = \$init; testSub($test_var); print "$$test_var\n$init\n";
    For me, that outputs:
    Hello!;) Hello!;)
    If you specifically need to see the XS file that Inline::C autogenerated for this particular exercise, you'll find it in the build directory (./_Inline/build).

    Cheers,
    Rob
      thanks to all!)
Re: xs and references
by Anonymous Monk on Feb 27, 2009 at 13:17 UTC
    of course it doesn't work , its incomplete :)

    Try useing sv_setpv.