Discussion:
calling perl from C
l***@public.gmane.org
2012-03-05 11:01:36 UTC
Permalink
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/
David Mertens
2012-03-05 12:36:18 UTC
Permalink
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
Post by l***@public.gmane.org
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 {
}
__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/
--
"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
l***@public.gmane.org
2012-03-05 12:57:28 UTC
Permalink
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);
}
Message du 05/03/12 à 13h36
De : "David Mertens"
Objet : 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
Post by l***@public.gmane.org
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 {
}
__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/
--
"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
___________________________________________________________
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/
David Mertens
2012-03-05 14:39:07 UTC
Permalink
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-xs-RykgAxdMFBPhvxM+***@public.gmane.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");
}
l***@public.gmane.org
2012-03-05 15:07:58 UTC
Permalink
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
Message du 05/03/12 à 15h39
De : "David Mertens"
Objet : 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. :-)
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");
}
___________________________________________________________
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/
Xiao Yafeng
2012-03-06 05:57:17 UTC
Permalink
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.
Post by David Mertens
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
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");
}
Sisyphus
2012-03-06 09:09:59 UTC
Permalink
----- Original Message -----
From: <laurent.habib-***@public.gmane.org>
To: <inline-***@public.gmane.org>
Sent: Monday, March 05, 2012 10:01 PM
Subject: calling perl from C


Hello,

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

###########################################

Pleased you find it useful :-)

In addition to the other advice you've received, take a look at
C/t/10callback.t (in the Inline source distribution).
It contains a number of the 'perlcall' examples worked into a suitable
format for Inline::C.

(In fact, if I recall correctly, they're probably copy'n'pastes of those
examples.)

Cheers,
Rob

Loading...