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

In reply to Re^4: What does perl stack OPs really do? by llancet
in thread What does perl stack OPs really do? by llancet

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.