Ah, that really makes a lot of sense. Especially seeing your explanation about the second bug... the light bulb flicked to on for me.

My sincere thanks to both you and ikegami, for treading previously where I'm wandering now, and especially for taking the time to put me back on track.

Regarding the second question: I did read through perlcall, but a second run-through it seemed to work out better (there's a lot to remember when diving into all this at first). Here's a working (correctly, I think) example of the (resolved) question. It's a basic example within an advanced topic, I suppose. All we're doing is passing an integer from Perl into a C function, which then calls a Perl function, passes the integer to it, accepts the integer back from the Perl function, and then the C function returns it back to Perl. A round-trip:

use strict; use warnings; use Inline C => 'DATA'; my $pass_param = 42; my $ret = test_perlcall( $pass_param ); print "And test_perlcall( $pass_param ) returned $ret\n"; sub return_input { shift; } __DATA__ __C__ int test_perlcall( int input ) { int rv; int count; dSP; /* Declare and initialize local copy of Perl stack ptr */ printf( "Called test_perlcall( %i )\n", input ); /* Create a boundary for temps we create. */ ENTER; SAVETMPS; /* Make mental note of current stack ptr (even if no params) */ PUSHMARK(SP); /* Push a mortal param onto the stack. */ XPUSHs(sv_2mortal(newSViv( input ))); /* Sync the global stack pointer up to our local */ PUTBACK; /* Call the Perl function; keep note of 'count' to test it. */ /* G_SCALAR flag meaning we want a scalar returned. */ count = call_pv( "return_input", G_SCALAR ); /* Refresh local copy of stack pointer because */ /* it may have been invalidated by call_pv() */ SPAGAIN; /* We never want the Perl stack to fall into an */ /* inconsistent state, so we must verify that we */ /* got exactly one RV where we expected exactly one. */ if( count != 1 ) croak( "Well, THAT failed to work.\n" ); /* Now that we know we got a RV, we can pop it off the stack. */ rv = POPi; printf( "C called Perl function, return_input( %i )\n", rv ); PUTBACK; /* Free our temps, leave the boundary we created earleir */ FREETMPS; LEAVE; return rv; /* Inline::C handles this typemap automatically. */ }

The output:

Called test_perlcall( 42 ) C called Perl function, return_input( 42 ) And test_perlcall( 42 ) returned 42

I post it here just because nobody likes to hear "I got it working.", without seeing what works. I peppered the C code with notes from my read of perlcall. Maybe it will be helpful to someone else stumbling into this thread in the future.

Updated... Moved declarations to top of the C sub as suggested by BrowserUk to maintain compatibility with strict C compilers.


Dave


In reply to Re^2: When to (and not to) use sv_2mortal() by davido
in thread When to (and not to) use sv_2mortal() by davido

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.