>
> Can you once more explain why you want a C program that
> executes Perl code given as a string?
Mainly learning purposes to better my mind
> And how should the Perl
> code be injected into the C program?
To illustrate it in Perl:
$perlcode = q(print "monkey";);
eval $perlcode;
Excpet in C
Explained a bit more below with the examples...
>
> You can always do
>
> const char *perlcode = "use CGI; ...";
>
> This however is tedious since you can't break literal C
> strings into several lines and have to escape double-quotes
> etc. There are probably a million ways to fill a string. It
> really depends on your requirements.
>
[snip]
> You could do:
>
> printf("%s", SvPV(eval_pv(perlcode)));
>
> > string = SvPV(ret, n_a);
> >
> > perl_destruct(my_perl);
> > perl_free(my_perl);
> >
> > /** Would this be better right after string =
> SvPV(...) or after perl_free() ?? **/
> > printf(string);
>
> Probably better before you destruct and free the Perl interpreter.
> SvPV() does not return a copy of the string in the scalar
> variable. It returns the pointer in it. So it could happen
> that the memory this pointer points to has been freed because
> the Perl interpreter has been destructed.
>
> How much of the allocated memory perl frees when exiting
> depends on the PERL_DESTRUCT_LEVEL (perl checks the value of
> this environment variable when exiting). Per default perl
> doesn't clean up very much and leaves this work to the
> operating system. Nonetheless, it's safer to access the
> perl-related memory before perl_free() happens.
>
>
Thanks for that awesome info Tassilo!!!!
Here's where I'm at so far everyone who's keeping score at home:
Ok, I can compile this example with:
cc -o perlemb perlemb.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
I can run it like this:
./perlemb -e 'print "Howdy";'
Howdy
Or
./perlemb
print "Howdy";
<cntrl D>
Howdy
Here's what I have on the inetrnal version (IE the perl to execute is provided inside
and not via cmd line)
#include <EXTERN.h> /* from the Perl distribution */
#include <perl.h> /* from the Perl distribution */
static PerlInterpreter *my_perl; /*** The Perl interpreter ***/
int main(int argc, char **argv, char **env)
{
my_perl = perl_alloc();
perl_construct(my_perl);
perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
perl_run(my_perl);
perl_destruct(my_perl);
perl_free(my_perl);
}
What I'd like to do with this is:
./perlemb2
Howdy
And eventually through using CGI's param() perhas:
./perlemb2 name=JoeMama
Howdy JoeMama
What I have so far for testing is:
#include <EXTERN.h>
#include <perl.h>
static PerlInterpreter *my_perl;
main (int argc, char **argv, char **env)
{
STRLEN n_a;
const char *perlcode = "use CGI 'header';print header();print 'hello World';";
my_perl = perl_alloc();
perl_construct( my_perl );
perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
perl_run(my_perl);
printf("%s", SvPV(eval_pv(perlcode)));
perl_destruct(my_perl);
perl_free(my_perl);
}
But when I compile it the same way as the first I get:
perlemb.c:18: macro `SvPV' used with just one arg
So I tried it as
printf("%s", SvPV(eval_pv(perlcode),n_a));
Which is how it was in another example and got:
perlemb.c: In function `main':
perlemb.c:18: invalid type argument of `->'
perlemb.c:18: invalid type argument of `->'
perlemb.c:18: invalid type argument of `->'
perlemb.c:18: warning: passing arg 1 of `Perl_sv_2pv' makes pointer from integer
without a cast
I'm sure I'm missing somethgin obvious but I'm fascinated with this!
TIA
Dan
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]