On Wed, Dec 03, 2003 at 09:35:54AM -0600 Dan Muey wrote:

> > On Tue, Dec 02, 2003 at 05:40:14PM -0600 Dan Muey wrote:
> > 
> > > What I was wondering about was how to execute some perl
> > code *inside*
> > > the c program instead of takign it via ARGV.  I  think eval_pv and
> > > eval_sv have somethgin to do with it but I'm a bit cloudy 
> > there. Since
> > > I don't know hardly anythgin about C I'll illustarte it in Perl.
> > > 
> > > my $perlcode = <<CODE;
> > >   # instead of getting this via command line argument
> > >   sub simplepage {
> > >           my $name = shift || 'world';
> > >           use CGI qw(header param);
> > >           print header()
> > >           print "I am perl hear me roar $name\n";
> > >           print insult();
> > >   }
> > >   sub insult { return "So is ytour mother\n"; }
> > >   return simplepage(param('name'));
> > > CODE
> > > 
> > > printf eval_pv($perldoc);
> > > 
> > > Basically execute bunch of code, from a one liner to a
> > bunch of lines,
> > > that basically ends with one return value(an html webpage) and then
> > > print that value out.
> > 
> > This can't work because you cannot have return-statements
> > outside of subroutines in Perl. However, why not just leave 
> > the 'return' off:
> > 
> >     my $perldocde = <<CODE;
> >         ...
> >         simplepage(param{name});
> >     CODE
> > 
> > And from C:
> > 
> >     STRLEN  n_a;
> >     char    *string;
> >     SV      *ret;
> >     ...
> >     ret = eval_pv(perlcode, TRUE);
>                         ^^^^^^^^
>       Is that a variable?

Yes. When I wrote 'char *string', I meant 'char *perlcode' actually.

>       How would one safely get some code into a C variable?
>       Like in Perl I can use qq() or similar or escape certain 
>       chacters. So double quotes don't kill me.

I can't really answer that because I don't understand what you want to
achieve. In the above, 'perlcode' is a plain C string containing the C
source. How you populate it is totally up to you.

Can you once more explain why you want a C program that executes Perl
code given as a string? And how should the Perl code be injected into
the C program?

> >     string = SvPV(ret, n_a);
> > 
> > eval_pv() returns the last statement evaluated as a SV.
> > 
> 
> Thanks for the insight!  So, if I understand it right (and assuming I
> compile it properly of course ;p) I would need something like this:
> [questions commented]
> 
>    #include <EXTERN.h>
>    #include <perl.h>
> 
>    static PerlInterpreter *my_perl;
> 
>    main (int argc, char **argv, char **env)
>    {
>        STRLEN n_a;
>        char    *string; /** added to perldoc example **/
>        SV      *ret;    /** added to perldoc example**/
> 
>        my_perl = perl_alloc();
>        perl_construct( my_perl );
> 
>        perl_parse(my_perl, NULL, 3, embedding, NULL);
>        perl_run(my_perl);
> 
>        ret = eval_pv(perlcode, TRUE); /** So how do I get my code safely into 
> perlcode ?? **/

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.

>        /** could I just printf(SvPV(...)) right here and shorten it a bit?? **/

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.

Tassilo
-- 
$_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-3(rellac(=_$({
pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(rehtona{tsuJbus#;
$_=reverse,s+(?<=sub).+q#q!'"qq.\t$&."'!#+sexisexiixesixeseg;y~\n~~dddd;eval


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to