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. */ } #### Called test_perlcall( 42 ) C called Perl function, return_input( 42 ) And test_perlcall( 42 ) returned 42