Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   "Incremental" Embedded Perl (http://www.velocityreviews.com/forums/t892509-incremental-embedded-perl.html)

Guenther Sohler 05-24-2005 05:49 AM

"Incremental" Embedded Perl
 
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 ?



Sherm Pendley 05-24-2005 06:43 AM

Re: "Incremental" Embedded Perl
 
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

Guenther Sohler 05-24-2005 08:28 AM

Re: "Incremental" Embedded Perl
 
On Tue, 24 May 2005 02:43:11 -0400, Sherm Pendley wrote:

It works perfectly, thank you for your help!




Anno Siegel 05-24-2005 11:24 AM

Re: "Incremental" Embedded Perl
 
Guenther Sohler <guenther.sohler@newlogic.com> 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


All times are GMT. The time now is 08:23 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.