DrMoisha has asked for the wisdom of the Perl Monks concerning the following question:

Dear Monks!

I'm now trying to call perl functions from c code. I've already built perl with MSVS2013 Professional on Windows 7 64 bit. I'm now testing perlembed code, and I've run into problem.

Let's look at Calling a Perl subroutine from your C program.

#include <EXTERN.h> #include <perl.h> static PerlInterpreter *my_perl; int main(int argc, char **argv, char **env) { char *args[] = { NULL }; PERL_SYS_INIT3(&argc,&argv,&env); my_perl = perl_alloc(); perl_construct(my_perl); perl_parse(my_perl, NULL, argc, argv, NULL); PL_exit_flags |= PERL_EXIT_DESTRUCT_END; /*** skipping perl_run() ***/ call_argv("showtime", G_DISCARD | G_NOARGS, args); perl_destruct(my_perl); perl_free(my_perl); PERL_SYS_TERM(); }
My trouble is if I call some sub, it only actually executes on perl_destruct(my_perl);. I did not found what causes such behaviour in perlembed. And I don't quite understand what is happening, so I tried place perl_run(my_perl), or GL_EVAL in different places.

Okay,- I've thought,- let's try Maintaining a persistent interpreter example. I don't want to bloat my question so here is link Maintaining-a-persistent-interpreter-example
This example crashes here:

if(SvTRUE(ERRSV)) fprintf(stderr, "eval error: %s\n", SvPV_nolen(ERRSV));
When I comment this out, it still does nothing, even on perl_destruct();

So my first question: is there any manual for me to understand, what happens when call_argv() is called? I'm now reading perlguts, perlcall, however, I cannot find it. If you can, please specify me right way.:)

And second question: when call_argv(); actually executes function? Or can I force somehow to do it? Thank you!


EDIT
Okay, function is called with call_argv(). It is problem with output. Print in function, does not show up until perl_destruct()
If print STDERR is used, output appears immediatly.
/EDIT

Replies are listed 'Best First'.
Re: When call_argv() actualy executes function
by syphilis (Archbishop) on Mar 31, 2015 at 13:12 UTC
    I've already built perl with MSVS2013 Professional on Windows 7 64 bit

    The Calling a Perl subroutine from your C program example works fine for me on Windows 7 64-bit.
    However, I'm using gcc compiler - so you're problem is either MSVC-related or related to the way you're trying to build and run the demo.
    Well ... I guess it could also be a perl version issue. I've tried it out only on perl-5.16.0 and 5.20.0.
    Which version of perl are you using ?

    Cheers,
    Rob
      I'm using latest 5.20.2. I'll try gcc.
Re: When call_argv() actualy executes function
by Corion (Patriarch) on Mar 31, 2015 at 12:41 UTC

    How did you run the first example? Note that the first example expects a Perl program to be loaded from the command line which implements a routine with the name showtime.

    It's weird that your second example crashes when accessing ERRSV because that variable should always exist.

    According to the documentation, call_argv immediately calls your function. Note that you need to deal with the return values yourself in C.

      Thank you for documentation link! Yes, I'm launching it from cmd like very_simple_example_that_does_not_works.exe showtime.pl. If I don't launch it like that - it shows error undefined subroutine