X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperlembed.pod;h=79783a7d30d651f412aada12d751fa33d53c7bb3;hb=1b3f7d2103791ceee4a17b0f9f5860baa1512c7a;hp=9111be125301b23a29161d76b60a461f44e2d133;hpb=54310121b442974721115f93666234a200f5c7e4;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perlembed.pod b/pod/perlembed.pod index 9111be1..79783a7 100644 --- a/pod/perlembed.pod +++ b/pod/perlembed.pod @@ -263,57 +263,49 @@ your C program>. =head2 Evaluating a Perl statement from your C program -One way to evaluate pieces of Perl code is to use -L. We've wrapped this inside our own -I function, which converts a command string to an SV, -passing this and the L flag to -L. - -Arguably, this is the only routine you'll ever need to execute -snippets of Perl code from within your C program. Your string can be +Perl provides two API functions to evaluate pieces of Perl code. +These are L and L. + +Arguably, these are the only routines you'll ever need to execute +snippets of Perl code from within your C program. Your code can be as long as you wish; it can contain multiple statements; it can employ L, L and L to include external Perl files. -Our I lets us evaluate individual Perl strings, and then +I lets us evaluate individual Perl strings, and then extract variables for coercion into C types. The following program, I, executes three Perl strings, extracting an C from the first, a C from the second, and a C from the third. #include #include - + static PerlInterpreter *my_perl; - - I32 perl_eval(char *string) - { - return perl_eval_sv(newSVpv(string,0), G_DISCARD); - } - + main (int argc, char **argv, char **env) { - char *embedding[] = { "", "-e", "0" }; - STRLEN length; - - my_perl = perl_alloc(); - perl_construct( my_perl ); - - perl_parse(my_perl, NULL, 3, embedding, NULL); - perl_run(my_perl); - /** Treat $a as an integer **/ - perl_eval("$a = 3; $a **= 2"); - printf("a = %d\n", SvIV(perl_get_sv("a", FALSE))); - - /** Treat $a as a float **/ - perl_eval("$a = 3.14; $a **= 2"); - printf("a = %f\n", SvNV(perl_get_sv("a", FALSE))); - - /** Treat $a as a string **/ - perl_eval("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a); "); - printf("a = %s\n", SvPV(perl_get_sv("a", FALSE), length)); - - perl_destruct(my_perl); - perl_free(my_perl); + char *embedding[] = { "", "-e", "0" }; + + my_perl = perl_alloc(); + perl_construct( my_perl ); + + perl_parse(my_perl, NULL, 3, embedding, NULL); + perl_run(my_perl); + + /** Treat $a as an integer **/ + perl_eval_pv("$a = 3; $a **= 2", TRUE); + printf("a = %d\n", SvIV(perl_get_sv("a", FALSE))); + + /** Treat $a as a float **/ + perl_eval_pv("$a = 3.14; $a **= 2", TRUE); + printf("a = %f\n", SvNV(perl_get_sv("a", FALSE))); + + /** Treat $a as a string **/ + perl_eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE); + printf("a = %s\n", SvPV(perl_get_sv("a", FALSE), na)); + + perl_destruct(my_perl); + perl_free(my_perl); } All of those strange functions with I in their names help convert Perl scalars to C types. They're described in L. @@ -326,10 +318,22 @@ I to create a string: a = 9.859600 a = Just Another Perl Hacker +In the example above, we've created a global variable to temporarily +store the computed value of our eval'd expression. It is also +possible and in most cases a better strategy to fetch the return value +from L instead. Example: + + ... + SV *val = perl_eval_pv("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE); + printf("%s\n", SvPV(val,na)); + ... + +This way, we avoid namespace pollution by not creating global +variables and we've simplified our code as well. =head2 Performing Perl pattern matches and substitutions from your C program -Our I lets us evaluate strings of Perl code, so we can +The I function lets us evaluate strings of Perl code, so we can define some functions that use it to "specialize" in matches and substitutions: I, I, and I. @@ -360,10 +364,7 @@ been wrapped here): #include static PerlInterpreter *my_perl; - I32 perl_eval(char *string) - { - return perl_eval_sv(newSVpv(string,0), G_DISCARD); - } + /** match(string, pattern) ** ** Used for matches in a scalar context. @@ -376,7 +377,7 @@ been wrapped here): command = malloc(sizeof(char) * strlen(string) + strlen(pattern) + 37); sprintf(command, "$string = '%s'; $return = $string =~ %s", string, pattern); - perl_eval(command); + perl_eval_pv(command, TRUE); free(command); return SvIV(perl_get_sv("return", FALSE)); } @@ -394,7 +395,7 @@ been wrapped here): command = malloc(sizeof(char) * strlen(*string) + strlen(pattern) + 35); sprintf(command, "$string = '%s'; $ret = ($string =~ %s)", *string, pattern); - perl_eval(command); + perl_eval_pv(command, TRUE); free(command); *string = SvPV(perl_get_sv("string", FALSE), length); return SvIV(perl_get_sv("ret", FALSE)); @@ -417,7 +418,7 @@ been wrapped here): command = malloc(sizeof(char) * strlen(string) + strlen(pattern) + 38); sprintf(command, "$string = '%s'; @array = ($string =~ %s)", string, pattern); - perl_eval(command); + perl_eval_pv(command, TRUE); free(command); array = perl_get_av("array", FALSE); num_matches = av_len(array) + 1; /** assume $[ is 0 **/ @@ -960,7 +961,7 @@ Dov Grobgeld, and Ilya Zakharevich. Check out Doug's article on embedding in Volume 1, Issue 4 of The Perl Journal. Info about TPJ is available from http://tpj.com. -February 1, 1997 +April 14, 1997 Some of this material is excerpted from Jon Orwant's book: I, Waite Group Press, 1996 (ISBN 1-57169-064-6) and appears