Here is an example. It's done in Inline but is just straight vanilla XS. The stack pointer behaves itself over the separate calls.

package Serialize; use Data::Dump; sub new { bless {foo=>'bar'}, shift } sub toString { return Data::Dump::dump($_[0]) } my $obj = new Serialize; my $str = "some string"; print serialize($obj), $/; print serialize($str), $/; use Inline Config => FORCE_BUILD => 1, CLEAN_AFTER_BUILD => 0; use Inline C => <<'END_OF_C_CODE'; void serialize(SV* sv_data) { int count; dXSARGS; sp = mark; printf("\nEntry SP is %d\n", sp); if (sv_isobject(sv_data)) { // this is an object so presume a toString method PUSHMARK(sp); XPUSHs(sv_data); // whack it back on the stack PUTBACK; count = perl_call_pv("toString", G_SCALAR); if ( count != 1 ) croak("Expected 1 value got %d\n", count); XPUSHs(sv_2mortal(newSVsv(POPs))); } else { // just return SV with something appended for this example sv_catpv(sv_data, " - serialized\0"); XPUSHs(sv_2mortal(newSVsv(sv_data))); } PUTBACK; printf("Exit SP is %d\n", sp); XSRETURN(1); } END_OF_C_CODE __DATA__ bless({ foo => "bar" }, "Serialize") some string - serialized Entry SP is 32203548 Exit SP is 32203552 Entry SP is 32203548 Exit SP is 32203552

In reply to Re^3: Problem on perl callbacks from XS by tachyon-II
in thread Problem on perl callbacks from XS by cosimo

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.