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.
-- "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
> 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
> On Mon, Mar 5, 2012 at 5:01 AM, wrote:
> > 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');
> -- > "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
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");
> Message du 05/03/12 à 15h39
> De : "David Mertens" > A : laurent.ha...@voila.fr
> Copie à : inl...@perl.org
> 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. :-)
> 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");
> }
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:
> 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");
> }
----- Original Message ----- From: <laurent.ha...@voila.fr>
To: <inl...@perl.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.)