in reply to Regexp and Inline::C

This depends on whether you are executing all this in a Perl or C context. If you are attempting to call a Perl subroutine from a C function which in turn was called from within a Perl script using the Inline::C module, you can use something like this to perform your task (largely based on code from Inline::C-Cookbook):

#!/usr/bin/perl use strict; use warnings; use Inline qw(C); c_func('AAA:BBB'); sub perl_sub { for (@_) { /^([^:]+):([^:]+)$/; print "$1 - $2\n"; } } __DATA__ __C__ void c_func(SV* text) { Inline_Stack_Vars; Inline_Stack_Push(newSVpvf("CCC:DDD")); Inline_Stack_Done; perl_call_pv("main::perl_sub", 0); Inline_Stack_Void; }

However, if your main() is a C function, I believe you need a different approach. I've never used it, but Inline::CPR ostensibly gives you a binary you can call from C which will handle the appropriate inheritance.

Replies are listed 'Best First'.
Re^2: Regexp and Inline::C
by syphilis (Archbishop) on Apr 15, 2010 at 23:35 UTC
    you can use something like this to perform your task (largely based on code from Inline::C-Cookbook)

    It's a bad example in the Cookbook, and thank you for reminding me that it needs to be fixed. Your code (and the Cookbook example) are fine if run just once - but if you call the Inline::C sub more than once, then things can go screwy.

    For example, with the code you posted, if I call it 10 times:
    c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB'); c_func('AAA:BBB');
    I get this output:
    AAA - BBB CCC - DDD CCC - DDD
    If I call the sub 10 times in a loop:
    for(1 .. 10) {c_func('AAA:BBB')}
    I get:
    Use of uninitialized value $1 in concatenation (.) or string at try.pl + line 10. Use of uninitialized value $2 in concatenation (.) or string at try.pl + line 10. - Use of uninitialized value $1 in concatenation (.) or string at try.pl + line 10. Use of uninitialized value $2 in concatenation (.) or string at try.pl + line 10. - AAA - BBB CCC - DDD Use of uninitialized value $1 in concatenation (.) or string at try.pl + line 10. Use of uninitialized value $2 in concatenation (.) or string at try.pl + line 10. - AAA - BBB CCC - DDD AAA - BBB CCC - DDD CCC - DDD
    The problem is that the Inline Stack macros are deficient for performing callbacks - and one should instead base one's callbacks on the perlcall documentation (from where my example was drawn).

    Cheers,
    Rob