Re: mixed language coding



On 06/19/01 Stefan Kamphausen wrote:
Jim Edwards writes:
 > 1.  Write all the gui in c and call the perl code from within callback
 > wrappers written in c.
 >     This seems like it would involve a lot of interface writing.
 > 
 > 2.  Write the part of the gui for the perl in perl and  the rest in c -
 > I think i would then need many fewer calls from c to perl
 >       possibly only two - one to setup the interface and another to
 > invoke the dialog.
 > 
 > Does anyone know of an example that does this and what kind of problems
 > i might encounter?

Actually I had just written a C programm that calls some perl routines
(for parsing files and extracting some information from that).
I'm not a pro at this so I used the pretty straightforward examples
from the perlguts, perlembed and perlxs man-pages.
Unfortunately this lacked memory like hell.

I posted this question to the PerlXS mailinglist:

http://lists.perl.org/showlist.cgi?name=perl-xs

at

http://archive.develooper.com/perl-xs perl org/msg00277.html

but never got a reply. So for the moment I have abandoned embedding
perl in C programs. Probably the other way round works better.

For an example of calling perl code from C in a way that doesn't leak,
try this program:
perl -MGtk -e '$i=0; Gtk->idle_add(sub {print "Hello world! ",++$i, "\n"}); Gtk->main'
After 1_311_166 iterations there wasn't any increase in the memory usage.
The difference here may be that the Gtk+ binding uses call_sv(),
you may try that instead. In fact changing you sample program to the
one below, not only it doesn't leak anymore, but it's several times
faster:-)

/* compile with */
/* gcc -o getconf getconf.c `perl -MExtUtils::Embed -e ccopts -e ldopts` */

/* I tried this under Perl 5.6.0 and perl 5.005_3 */
   
# include <stdio.h>
# include <EXTERN.h>
# include <perl.h>
# include <unistd.h>

void call_perl(void);
static PerlInterpreter *my_perl;

int main(int argc, char **argv, char **env)
{
   char *embedding[] = { "", "-e", "sub p {print \"Hello World\\n\"}" };
   long int i=0;
   my_perl = perl_alloc();
   perl_construct(my_perl);
   perl_parse(my_perl, NULL, 3, embedding, NULL);
   perl_run(my_perl);

   /* In a loop we leak */
   PL_perl_destruct_level = 1;  /* this is from perlmonks discussion */
   while (1) {
          printf("%ld ",i++);
          call_perl();
          /* sleep(1); */
   }
   perl_destruct(my_perl);
   perl_free(my_perl);
}

void call_perl(void) {
        dSP;
        PUSHMARK(SP);
        call_pv("p", G_DISCARD);
}

lupus

-- 
-----------------------------------------------------------------
lupus debian org                                     debian/rules
lupus ximian com                             Monkeys do it better




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]