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

>     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 
?? **/

         /** could I just printf(SvPV(...)) right here and shorten it a bit?? **/
       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); 
   }  

I got the first example (IE the -e "print "howdy";' version) working and am playing 
around with having it inside

> Tassilo
> --
> $_=q#",}])!JAPH!qq(tsuJ[{@"tnirp}3..0}_$;//::niam/s~=)]3[))_$-
> 3(rellac(=_$({
> pam{rekcahbus})(rekcah{lrePbus})(lreP{rehtonabus})!JAPH!qq(reh
> tona{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