Ramprasad A Padmanabhan wrote:

> I am writing a c program In which I have some heavy text parsing to do.
> Unfortunately I am not very conversant with C or Regex in C
> 
> I have read about embedding perl into C programs,
> 
> Now How Can I do this efficiently
> 
> Suppost I want to parse a Mail To: header and want to extract the emailid
> 
> like take an example
> 
> 
>   @strings = ('"Ram Prasad" <[EMAIL PROTECTED]> ',
> '[EMAIL PROTECTED],[EMAIL PROTECTED]');
> 
> foreach (@strings) {
> while(/([EMAIL PROTECTED])/g){
> push @emailids , $1;
>          }
> }
> 
> print "GOT ALL EMAILIDS in @emailids\n";
> 
> __END__
> 
> Now think of doing that in C.  I will have to spend two weeks learning C
> regex libraries before I can do this
> 
> Instead Can I just embed this perl code  in my c program
> I will be happy if someone can give some links to examples on the net

perldoc perlembed
perldoc perlxs
perldoc perlapi

is what you will need to embed your Perl program in C. as a simple example, 
the following embeds a Perl script, test.pl, into a C program test.c. 
test.c simply runs a regexp and extract the emails address that it finds in 
test.pl:

#--
#-- test.pl
#--
$email = '[EMAIL PROTECTED],[EMAIL PROTECTED]';

#--
#-- the following is for demo puprose, don't use this 
#-- for extracting email address out of a string. you
#-- can embed any Perl code you want here instead.
#--
@emails = $email =~ /([^,[EMAIL PROTECTED],]+)/g;

__END__

the following C program will embed the above Perl script and prints
out the email address in @emails:

#include <EXTERN.h>
#include <perl.h>

static PerlInterpreter* perl;
static AV *emails;

int main(int argc,char** argv){

        int x;
        STRLEN y;

        perl = perl_alloc();
        perl_construct(perl);
        perl_parse(perl,NULL,argc,argv,NULL);
        perl_run(perl);

        // get @emails in test.pl
        emails = get_av("emails",0);

        // av_len(emails) is the same as $#emails
        for(x = 0; x <= av_len(emails); x++){

                // av_fetch(emails,x,0) is the same as $emails[$x]
                SV* email = *av_fetch(emails,x,0);

                // SvPV(email,y) "converts" an array element to a string
                printf("email %d is: %s\n",x+1,SvPV(email,y));
        }

        perl_destruct(perl);
        perl_free(perl);
}

to compile this C program, you will need to know how Perl is build
in your machine, the following usually does the trick:

[panda]# gcc test.c -o test `perl -MExtUtils::Embed -e ccopts -e ldops`

finally, to test your executable:

[panda]# ./test test.pl

assuming test.pl is in the same directory as test.c, you will see:

[EMAIL PROTECTED]
[EMAIL PROTECTED]

david
-- 
s,.*,<<,e,y,\n,,d,y,.s,10,,s
.ss.s.s...s.s....ss.....s.ss
s.sssss.sssss...s...s..s....
...s.ss..s.sss..ss.s....ss.s
s.sssss.s.ssss..ss.s....ss.s
..s..sss.sssss.ss.sss..ssss.
..sss....s.s....ss.s....ss.s

,....{4},"|?{*=}_'y!'+0!$&;"
,ge,y,!#:$_(-*[./<[EMAIL PROTECTED],b-t,
.y...,$~=q~=?,;^_#+?{~,,$~=~
y.!-&*-/:[EMAIL PROTECTED] ().;s,;,
);,g,s,s,$~s,g,y,y,%,,g,eval

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to