Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Regexp and Inline::C

by m0d (Novice)
on Apr 15, 2010 at 13:53 UTC ( #834890=perlquestion: print w/ replies, xml ) Need Help??
m0d has asked for the wisdom of the Perl Monks concerning the following question:

I need your wisdom. Could anyone give me an example how to use Regexp in the Inline::C module. I checked the perlapi but i did not get it. How would you code that with the perlapi
my $string = "AAA:BBB"; $string =~ /^([^:]+):([^:]+)$/; print "$1 - $2\n";

Comment on Regexp and Inline::C
Download Code
Re: Regexp and Inline::C
by syphilis (Canon) on Apr 15, 2010 at 14:26 UTC
    How would you code that with the perlapi

    Possibly not what you're after, but I think *I* would actually do it as a callback:
    use warnings; use Inline C => <<'EOC'; void C_foo(char *a) { dSP; ENTER; SAVETMPS; PUSHMARK(SP); POPMARK; PUSHMARK(SP); XPUSHs(sv_2mortal(newSVpv(a, 0))); PUTBACK; call_pv("foo", G_DISCARD); FREETMPS; LEAVE; } EOC my $string = "AAA:BBB"; C_foo($string); sub foo { $_[0] =~ /^([^:]+):([^:]+)$/; print "$1 - $2\n"; }
    Cheers,
    Rob
Re: Regexp and Inline::C
by kennethk (Monsignor) on Apr 15, 2010 at 14:34 UTC
    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.

      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
Re: Regexp and Inline::C
by m0d (Novice) on Apr 15, 2010 at 15:01 UTC
    I thank you guys. This helped me much. My intention was to subsitute a pure perl function with a function written in C.

      You can't call Perl operators (from C or anywhere), just functions.

      The regex pattern doesn't have to be hardcoded — it could be passed as an argument — but if you want to call Perl's match operator, you'll need to call a Perl function that contains it.

Re: Regexp and Inline::C
by creamygoodness (Curate) on Apr 15, 2010 at 16:48 UTC
    I checked the perlapi but i did not get it.

    The C regex engine hooks are not documented in perlapi, they're elsewhere. See perlreapi and perlreguts.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834890]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (9)
As of 2014-12-19 12:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (82 votes), past polls