As I made up a wrapper, the real code is pretty long.
This is the perl code:
{
my $static_var = 0;
sub return_op {
say "in return_op: $static_var";
return $static_var;
}
}
This is the main function:
int main(.......) {
......
// return value test
{
cout<<"### return value test"<<endl;
pppStack re1 = interp->call("return_op",true,false);
pppStack re2 = interp->call("return_op",true,false);
}
,,,,,,
}
And this is the code in Interp class that do the calling:
class pppInterp
{
public:
......// a lot of methods here
pppStack call(string subname, bool want_re, bool want_array);
pppStack call(string subname, pppStack& args, bool want_re, bool w
+ant_array);
protected:
// the interpreter only has this one property
static bool inited;
PerlInterpreter* my_perl;
}
//
// the method implementation
//
pppStack pppInterp::call(string subname, bool want_re, bool want_array
+)
{
pppStack empty;
return call(subname,empty,want_re,want_array);
}
pppStack pppInterp::call(string subname, pppStack& args, bool want_re,
+ bool want_array)
{
// enter sub
SPAGAIN;
ENTER;
SAVETMPS;
// push args
PUSHMARK(SP);
pppStack::iterator it;
int arg_count = 0;
for (it=args.begin(); it!=args.end(); it++) {
SV* arg_copy = newSVsv((*it)->get_internal());
sv_2mortal(arg_copy);
XPUSHs(arg_copy);
arg_count++;
}
PUTBACK;
// call sub
I32 flags = G_EVAL;
if (arg_count==0) flags |= G_NOARGS;
if (!want_re) flags |= G_DISCARD;
if (want_array) flags |= G_ARRAY;
else flags |= G_SCALAR;
int re_count = call_pv(subname.c_str(),flags);
// check eval
if (SvTRUE(ERRSV)) {
if (want_array) POPs;
throw(SvPV_nolen(ERRSV));
}
// fetch args
pppStack re;
if (want_re) {
SPAGAIN;
int i;
for (i=0; i<re_count; i++) {
SV* tmp = POPs;
Perl_sv_dump(my_perl,tmp);
re.push_front( create_sv(tmp) );
}
}
PUTBACK;
// exit sub
FREETMPS;
LEAVE;
return re;
}
at last, it gives the return value like this. You can see, each time of calling gives me a different SV. It is printed immediately after POPs.
### return value test
in return_op: 0
SV = PVIV(0xffd228) at 0xfebda0
REFCNT = 1
FLAGS = (TEMP,IOK,POK,pIOK,pPOK)
IV = 0
PV = 0x1013fd0 "0"\0
CUR = 1
LEN = 8
in return_op: 0
SV = PVIV(0xffd240) at 0xfebf80
REFCNT = 1
FLAGS = (TEMP,IOK,POK,pIOK,pPOK)
IV = 0
PV = 0x10102b0 "0"\0
CUR = 1
LEN = 8
|