"Incremental" Embedded Perl

Discussion in 'Perl Misc' started by Guenther Sohler, May 24, 2005.

  1. I have an Application using embedded Perl

    it works in following steps

    1) I allocate and create perl
    2) I parse my perl script with the perl interpreter (perl_parse)
    3) I definde my own functions using newXS
    4) I run the perl script
    5) Eventually some perl functions are called from within my c code
    which properly do their job - all find

    6) My program has the ability to run custom perl scripts. In this case
    continue at 2
    perl_parse is called again to read in the new perl script

    7) Unfortunately all the other context already read is deleted. Varibles,
    functions ... that means the fucntions called at 5 will fail


    Is it possible to incrementally load perl scripts using perl_parse
    eg if I load script1 which defines a=5, I parse and execute it, so a
    becomes 5 ; then to parse script2 which prints out a, and then run script2
    to see that a is set to 5.
    How would it work ? what do I have to do ?
     
    Guenther Sohler, May 24, 2005
    #1
    1. Advertising

  2. Guenther Sohler wrote:

    > 1) I allocate and create perl
    > 2) I parse my perl script with the perl interpreter (perl_parse)
    > 3) I definde my own functions using newXS
    > 4) I run the perl script
    > 5) Eventually some perl functions are called from within my c code
    > which properly do their job - all find
    >
    > 6) My program has the ability to run custom perl scripts. In this case
    > continue at 2
    > perl_parse is called again to read in the new perl script
    >
    > 7) Unfortunately all the other context already read is deleted. Varibles,
    > functions ... that means the fucntions called at 5 will fail
    >
    >
    > Is it possible to incrementally load perl scripts using perl_parse
    > eg if I load script1 which defines a=5, I parse and execute it, so a
    > becomes 5 ; then to parse script2 which prints out a, and then run script2
    > to see that a is set to 5.
    > How would it work ? what do I have to do ?


    The details vary according to your specific application, but the gist of
    it will usually remain the same: You call perl_parse() and perl_run()
    once, as part of creating and initializing the Perl interpreter. After
    that, you call one or more of the eval_*() functions as many times as
    you need to.

    Let's start with the simplest possible Perl program:

    char *emb = { "", "-e", "1" };
    PerlInterpreter *pi = perl_alloc();
    perl_construct(pi);
    perl_parse(pi, xs_init, 3, emb, (char**)NULL);
    perl_run(pi);

    After you've done this, don't release or deallocate the Perl interpreter
    you've created here, and don't call perl_parse() or perl_run() again.
    Instead, use one of the several eval_*() functions whenever you want to
    parse and run any additional Perl code.

    For instance:

    dTHX;
    char *p = get_some_perl_code(); // Assume this reads some Perl
    // from a file
    SV *result = eval_pv(p, TRUE);
    if (SvTRUE(ERRSV)) {
    printf("Perl error: %s", SvPV(ERRSV, PL_na));
    }

    This is roughly equivalent to using eval() to catch errors, and then
    checking for $@. You could also allow Perl to die() on errors by passing
    FALSE as the second argument to eval_pv().

    You could also use one of the other eval_*() functions to call a named
    Perl function, a method, or a code ref, instead of passing the Perl code
    as a string of text.

    If you've gotten this far, I assume you're already quite familiar with
    the relevant PODs, but for the sake of the archives:

    perlembed Perl ways to embed perl in your C or C++ application
    perldebguts Perl debugging guts and tips
    perlxstut Perl XS tutorial
    perlxs Perl XS application programming interface
    perlclib Internal replacements for standard C library functions
    perlguts Perl internal functions for those doing extensions
    perlcall Perl calling conventions from C

    perlapi Perl API listing (autogenerated)
    perlintern Perl internal functions (autogenerated)
    perliol C API for Perl's implementation of IO in Layers
    perlapio Perl internal IO abstraction interface

    perlhack Perl hackers guide

    This list is taken from 5.8.6 - if you're using an older Perl, it might
    not have all of the PODs listed above.

    sherm--

    --
    Cocoa programming in Perl: http://camelbones.sourceforge.net
    Hire me! My resume: http://www.dot-app.org
     
    Sherm Pendley, May 24, 2005
    #2
    1. Advertising

  3. On Tue, 24 May 2005 02:43:11 -0400, Sherm Pendley wrote:

    It works perfectly, thank you for your help!
     
    Guenther Sohler, May 24, 2005
    #3
  4. Guenther Sohler

    Anno Siegel Guest

    Guenther Sohler <> wrote in comp.lang.perl.misc:
    > I have an Application using embedded Perl
    >
    > it works in following steps
    >
    > 1) I allocate and create perl
    > 2) I parse my perl script with the perl interpreter (perl_parse)
    > 3) I definde my own functions using newXS
    > 4) I run the perl script
    > 5) Eventually some perl functions are called from within my c code
    > which properly do their job - all find
    >
    > 6) My program has the ability to run custom perl scripts. In this case
    > continue at 2
    > perl_parse is called again to read in the new perl script
    >
    > 7) Unfortunately all the other context already read is deleted. Varibles,
    > functions ... that means the fucntions called at 5 will fail
    >
    >
    > Is it possible to incrementally load perl scripts using perl_parse
    > eg if I load script1 which defines a=5, I parse and execute it, so a
    > becomes 5 ; then to parse script2 which prints out a, and then run script2
    > to see that a is set to 5.
    > How would it work ? what do I have to do ?


    Study the pertinent documentation. I'm pretty sure I read something
    about your problem when I looked up the answer to your last question.
    I'm not going back to find it again.

    In another reply, Sherm Pendley has listed the documents that concern
    embedding Perl. Yes, it's a lot to read. Read them anyway, instead
    of asking the group to do it for you.

    Anno
     
    Anno Siegel, May 24, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Mark Goldin

    Incremental search

    Mark Goldin, Jul 26, 2004, in forum: ASP .Net
    Replies:
    6
    Views:
    2,968
    rxiong
    Aug 25, 2010
  2. Luis Ferrao

    DataGrid: iTunes-like incremental search

    Luis Ferrao, Jan 10, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    986
    Luis Ferrao
    Jan 10, 2005
  3. Kevin McMurtrie

    Need help with incremental GC failure

    Kevin McMurtrie, Jun 19, 2004, in forum: Java
    Replies:
    0
    Views:
    664
    Kevin McMurtrie
    Jun 19, 2004
  4. Kevin McMurtrie

    Need help with incremental GC failure

    Kevin McMurtrie, Jun 19, 2004, in forum: Java
    Replies:
    0
    Views:
    648
    Kevin McMurtrie
    Jun 19, 2004
  5. David
    Replies:
    0
    Views:
    362
    David
    Mar 1, 2005
Loading...

Share This Page