"Incremental" Embedded Perl

G

Guenther Sohler

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 ?
 
S

Sherm Pendley

Guenther said:
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--
 
G

Guenther Sohler

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

It works perfectly, thank you for your help!
 
A

Anno Siegel

Guenther Sohler said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top