#! perl -slw use strict; use Inline C => <<'END_C', NAME => 'varArgsC2P', CLEAN_AFTER_BUILD => 0; void foo (int first, ...) { dSP ; int next; va_list argp; ENTER ; SAVETMPS ; PUSHMARK(SP) ; va_start( argp, first ); XPUSHs( sv_2mortal( newSViv( first ) ) ); while( ( next = va_arg( argp, int ) ) != -1 ) { XPUSHs( sv_2mortal( newSViv( next ) ) ); // printf( "Stacking %d \n", next ); } va_end( argp ); PUTBACK; perl_call_pv( "main::test", G_SCALAR ); // SPAGAIN; FREETMPS; LEAVE; } void call_foo( int n ) { switch( n ) { case 1: foo( 1, -1 ); break; case 2: foo( 1,2, -1 ); break; case 3: foo( 1,2,3, -1 ); break; case 4: foo( 1,2,3,4, -1 ); break; case 5: foo( 1,2,3,4,5, -1 ); break; case 6: foo( 1,2,3,4,5,6, -1 ); break; case 7: foo( 1,2,3,4,5,6,7, -1 ); break; case 8: foo( 1,2,3,4,5,6,7,8, -1 ); break; case 9: foo( 1,2,3,4,5,6,7,8,9, -1 ); break; case 10: foo( 1,2,3,4,5,6,7,8,9,10, -1 ); break; } return; } END_C sub test { printf "test called with %d args: [@_]\n", scalar @_; } call_foo( $_ ) for 1 .. 10; __END__ C:\test>varArgsC2P.pl test called with 1 args: [1] test called with 2 args: [1 2] test called with 3 args: [1 2 3] test called with 4 args: [1 2 3 4] test called with 5 args: [1 2 3 4 5] test called with 6 args: [1 2 3 4 5 6] test called with 7 args: [1 2 3 4 5 6 7] test called with 8 args: [1 2 3 4 5 6 7 8] test called with 9 args: [1 2 3 4 5 6 7 8 9] test called with 10 args: [1 2 3 4 5 6 7 8 9 10]