http://qs1969.pair.com?node_id=517265

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

Hi monks,

I am calling a perl subroutine from a C program. I have 4 questions

(1) My first question is --what is the need of "perl_run" if i am using "perl_call_argv" to call the perl subroutine . The reason why i am asking is that when i initially used "perl_call_arv" only, i got error like free(): invalid pointer 0x804c690!But when i called "perl_run" before "perl_destruct" the error vanished.Why this happened. When i referred to perldoc it says "in general it's considered good form so that DESTROY methods and END blocks are executed at the right time."

(2) My second question is about the third argument in "perl_parse".Some people put it as 2 or 3 or argc. I want to know what it actually means.

(3) My third question is ---I have to call 2 different perl subroutines in 2 different perl files from a C file. So i want to know whether i will to have initialize 2 seperate occurances of "PerlInterpreter" . If it is possible to use same "PerlInterpreter" for calling 2 different perl files ,could you please specify how.

(4) Is there any other documentaion for perlembed,perlguts,perlcall other than perldoc. Wherever i search in internet people have copied from perldoc and put it in another form.Could any one help me to find a documentation other than perldoc.You people being monks in perl could easily do with PERLDOC.But novices like me cannot.So please

Replies are listed 'Best First'.
Re: Doubts of a perl beginner on PERLEMBED
by xdg (Monsignor) on Dec 16, 2005 at 15:54 UTC

    For #4, while I haven't read it, someone (chromatic?) pointed out to me that there is a book on the topic: Extending and Embedding Perl.

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

Re: Doubts of a perl beginner on PERLEMBED
by robin (Chaplain) on Dec 16, 2005 at 18:31 UTC
    (2) My second question is about the third argument in "perl_parse".Some people put it as 2 or 3 or argc. I want to know what it actually means.
    The third argument (argc) is simply the number of elements in the fourth argument (argv). This is an array of strings, which corresponds to the arguments you pass to perl on the command line.

    In the examples in perlembed, some of them just pass their own command-line arguments through to the Perl interpreter, hence they pass their own argc and argv, while others use a constant array of arguments, and pass its length (which happens to be 3 in one of the examples, because the array is { "", "-e", "0" }, and 2 in another because the array is { "", "power.pl" }.)

    (The first element of argv is more or less ignored, which is why these examples use the empty string. I imagine it's used to initialise $0, but I haven't confirmed that. It’s not even used to initialise $0.)

    Update: changed ‘argument’ to the less ambiguous ‘element of argv’, in the last sentence.

Re: Doubts of a perl beginner on PERLEMBED
by robin (Chaplain) on Dec 16, 2005 at 22:47 UTC
    (3) My third question is ---I have to call 2 different perl subroutines in 2 different perl files from a C file. So i want to know whether i will to have initialize 2 seperate occurances of "PerlInterpreter" . If it is possible to use same "PerlInterpreter" for calling 2 different perl files ,could you please specify how.
    Is there any reason you can't just combine the two files into one?

    You can probably load both files into the same perl interpreter using require. So something like

    char *my_args = { "perl", "-e", "require 'file1.pl'; require 'file +2.pl'" }; ... perl_parse(my_perl, NULL, 3, my_args, (char **)NULL);
    might well work for you. (Of course it will cause problems if both files use the same subroutine name.)
      Hi Robin,

      Thanks for the good & useful replies. I will not be able to combine 2 perl files into one . And one of my aims is to invoke the perl interpreter only once. In your example i have seen "perl" as the first argument for "my_args". What does that signify? .Usually people put that field as empty ("") .And one more thing is that whether specifying "perl" as the first argument of "my_argv" may cause the perl intepreter to be invoked every time i call a perl subroutine.
        You're being rather mysterious, which makes it harder to give helpful answers. (Why can't you combine the two scripts into one? Why do you aim to invoke the Perl interpreter only once?)

        I don't think the first element of argv is used. I prefer to set it to ‘perl’ because, logically, it’s supposed to be the name that the program was invoked with. But set it to ‘fiddlesticks’ if you prefer – I don’t think it will make any difference to the result.

      Hi ,
      Sorry for disturbing you again. As I had to call subroutine named "test" in file 'file1.pl i used
      char *my_argv[] = {"","-e","require 'file1.pl';require 'file2.pl';" }
      and then used
      call_argv("test",G_DISCARD,args);
      But i am getting error Undefined subroutine &main::test called.What could be the reason

      UPDATE : The error was not shown when i included "perl_run" after perl_parse.What could be the possible reason?
        The files file1.pl and file2.pl are loaded by the require statements. If you don't execute the require statements (by running the script) then the files won't be loaded. Simply compiling the one-liner isn't enough to load the files.
Re: Doubts of a perl beginner on PERLEMBED
by Anonymous Monk on Dec 16, 2005 at 17:25 UTC

    Just out of curiousity, your title says:

    Doubts of a perl beginner on PERLEMBED

    But where in the post are you expressing any doubts? It makes me suspicious to see posts with misleading titles, and I think the title you've chosen is a bit misleading (whether intentionally or not).

      It makes me suspicious
      Suspicious of what? Your use of the word "suspicious" makes me suspicious in the same way that you're suspicious of the OP. Confused yet? Let's hope so.

      thor

      The only easy day was yesterday