Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
calling perl from C
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  7 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
laurent.ha...@voila.fr  
View profile  
 More options Mar 5 2012, 6:01 am
Newsgroups: perl.inline
From: laurent.ha...@voila.fr
Date: Mon, 5 Mar 2012 12:01:36 +0100 (CET)
Local: Mon, Mar 5 2012 6:01 am
Subject: calling perl from C
Hello,

First, thanks for the Inline::C package, which is well build, and very usefull for me.

I would like that "C code"   call   "perl sub"    but    many times.

When I execute the script I have a "panic: memory wrap"...

Is there a solution to do that...

Thanks.

Laurent

 use Inline C;
# use Inline 'NoClean', 'FORCE', 'INFO' ;
# use Inline Config => WARNINGS => 4;

use strict;

# c_func_1('This is the first line');
c_func_2('This is the second line');
   
    sub perl_sub_1 {
        print map "$_\n", @_;
    }
   
__DATA__
__C__
   
void c_func_1(SV* text) {
 c_func_2(text);

}

   
void c_func_2(SV* text) {

int i = 0;

for (i=0;i<10;i++)
{
 perl_call_pv("main::perl_sub_1", 0);
 //SPAGAIN;

}
}

void cont (void) {
}

___________________________________________________________
Les 10 aliments pour lutter contre le rhume ou la grippe sont sur Voila.fr http://actu.voila.fr/evenementiel/sante-bien-etre/aliments-anti-rhume/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mertens  
View profile  
 More options Mar 5 2012, 7:36 am
Newsgroups: perl.inline
From: dcmertens.p...@gmail.com (David Mertens)
Date: Mon, 5 Mar 2012 06:36:18 -0600
Local: Mon, Mar 5 2012 7:36 am
Subject: Re: calling perl from C
Laurent -

Have you seen perlcall? http://perldoc.perl.org/perlcall.html

Check out the examples section and see if they make sense. If not, let us
know and we'll see if something is broken. FWIW, I've called Perl subs from
my C code before, so I know it's possible.

David

--
 "Debugging is twice as hard as writing the code in the first place.
  Therefore, if you write the code as cleverly as possible, you are,
  by definition, not smart enough to debug it." -- Brian Kernighan

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
laurent.ha...@voila.fr  
View profile  
 More options Mar 5 2012, 7:57 am
Newsgroups: perl.inline
From: laurent.ha...@voila.fr
Date: Mon, 5 Mar 2012 13:57:28 +0100 (CET)
Local: Mon, Mar 5 2012 7:57 am
Subject: Re: calling perl from C
David,

I have ever seen perlcall, there is an example for calling perl from C but once. (I test it  :: OK)

When, C call perl, the sub is executed and did'nt return to C (memory wrap).

May be a problem of c ontext.

My interpretation is when C had call Perl, Perl is executed and have no reference to return to C code. (:-) )

For information, i'm writing a routine in C to remplace ualarm which did'nt work on Perl under windows.

Laurent

 for (i=0;i<10;i++)
{
perl_call_pv("main::perl_sub_1", 0);

___________________________________________________________
Les 10 aliments pour lutter contre le rhume ou la grippe sont sur Voila.fr http://actu.voila.fr/evenementiel/sante-bien-etre/aliments-anti-rhume/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
David Mertens  
View profile  
 More options Mar 5 2012, 9:39 am
Newsgroups: perl.inline
From: dcmertens.p...@gmail.com (David Mertens)
Date: Mon, 5 Mar 2012 08:39:07 -0600
Local: Mon, Mar 5 2012 9:39 am
Subject: Re: calling perl from C
Laurent,

Extending the example in perlcall to work for repeated calls to Perl is not
hard, but you have clearly taken the wrong path. In particular, you should
have "dSP;" and "PUSHMARK(SP);" at least once in your code, and I see
you've removed them completely. Sometimes it's just hard to put all the
pieces together, and perhaps the language barrier is causing trouble.

I've included a working solution below that takes a Perl subroutine
(reference, not name) and calls it the requested number of times. It also
prints out some diagnostic output from C so that you can see the flow of
the code. Extending this to take a subroutine name instead of a subref, or
passing arguments, is left as an exercise to the reader. :-)

If you have more questions about XS which perlcall does not explain, you
might consider signing up for perl...@perl.org. It's a very low-volume list
that might be better targeted to your XS questions.

David

use strict;
use warnings;
use Inline 'C';

my $n_times_called = 0;
sub my_perl_func {
    $n_times_called++;
    print "Called ${n_times_called}th time\n";

}

call_from_c(\&my_perl_func, 10);

__DATA__
__C__

void call_from_c(SV * subref, int n_times) {
    int i;
    printf("Starting C function\n");
    for (i = 0; i < n_times; i++) {
        dSP;
        PUSHMARK(SP);
        printf("Calling Perl function...\n");
        call_sv(subref, G_DISCARD|G_NOARGS);
        printf("I'm back\n");
    }
    printf("Done with C function\n");


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "calling perl from C [CLOSED]" by laurent.ha...@voila.fr
laurent.ha...@voila.fr  
View profile  
 More options Mar 5 2012, 10:07 am
Newsgroups: perl.inline
From: laurent.ha...@voila.fr
Date: Mon, 5 Mar 2012 16:07:58 +0100 (CET)
Local: Mon, Mar 5 2012 10:07 am
Subject: Re: calling perl from C [CLOSED]
Hye,

I had try to put PUSHMARK without DSP ; -)

I have test you're exemple which work well.

I take a look to perlcall and xs.

Thanks (a lot!)

:-)

Laurent

___________________________________________________________
Les 10 aliments pour lutter contre le rhume ou la grippe sont sur Voila.fr http://actu.voila.fr/evenementiel/sante-bien-etre/aliments-anti-rhume/

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Discussion subject changed to "calling perl from C" by Xiao Yafeng
Xiao Yafeng  
View profile  
 More options Mar 6 2012, 12:57 am
Newsgroups: perl.inline
From: xyf.x...@gmail.com (Xiao Yafeng)
Date: Tue, 6 Mar 2012 13:57:17 +0800
Local: Tues, Mar 6 2012 12:57 am
Subject: Re: calling perl from C
Laurent,

Like David said, before get thing you want to do done, you should
understand how perl script runs first. perlcall, perlembed and perlguts
would help you.

On Mon, Mar 5, 2012 at 10:39 PM, David Mertens <dcmertens.p...@gmail.com>wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
"Sisyphus"  
View profile  
 More options Mar 6 2012, 4:09 am
Newsgroups: perl.inline
From: sisyph...@optusnet.com.au ("Sisyphus")
Date: Tue, 6 Mar 2012 20:09:59 +1100
Local: Tues, Mar 6 2012 4:09 am
Subject: Re: calling perl from C


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »