int getStringFromPerlSubroutine(PerlInterpreter* perlInterpreter, char* packageExplicitSubName, SV** string) { PERL_SET_CONTEXT(perlInterpreter); dSP; // Initialize local stack pointer everything created after here. ENTER; SAVETMPS; // This is a temporary variable. PUSHMARK(SP); // Remember the stack pointer. PUTBACK; // Make local the stack pointer global. if (call_pv(packageExplicitSubName, G_SCALAR | G_NOARGS | G_EVAL) == 0) { // TODO: Interpret return value for success. //fprintf(stderr, "Error: call_pv did not return 1 as it should have. \n"); //return 1; } SPAGAIN; // Refresh the stack pointer. // Check there were no problems in the subroutine. if (SvTRUE(ERRSV)) { STRLEN n_a; fprintf(stderr, "Error: getSvPV %s. \n", SvPV(ERRSV, n_a)); POPs; return 1; } STRLEN n_a; SV* stringRV = newRV_inc(POPs); // Check returned SV is a reference. if (!SvROK(stringRV)) { fprintf(stderr, "Error: getSvPV could not get a reference to the SVt_PV expected to be returned by the Perl subroutine. \n"); return 1; } // Check the type of the reference if (SvTYPE(SvRV(stringRV)) != SVt_PV) { fprintf(stderr, "Error: getSvPV was not returned a reference to an SVt_PV from the Perl subroutine as expected. (Actual enum: %d) \n", SvTYPE(SvRV(stringRV)) ); return 1; } *string = SvRV(stringRV); //printf("%s \n", SvPV(tempPV, n_a)); //*string = SV* stringSV = newSVpx(POPpx, n_a); //printf("Inside %s \n", SvPV(*string, n_a)); //(*string->sv_refcnt)++; PUTBACK; FREETMPS; // Free that return value. LEAVE; // Free the XPUSHed "mortal" args. return 0; } int main() { PerlInterpreter *perlInterpreter; //Init perlInt... STRLEN len; SV* string = newSV(sizeof(SV*)); getStringFromPerlSubroutine(perlInterpreter, "TestPackage::getString", &string); printf("%s \n", SvPV(string, len)); printf("Length of returned string: %d", len); SvREFCNT_dec(string); // Cleanup perlInt... return 0; }