From 137443ea0a858c43f5a720730cac6209a7d41948 Mon Sep 17 00:00:00 2001 From: Perl 5 Porters Date: Tue, 15 Apr 1997 00:00:00 +1200 Subject: [inseparable changes from patch from perl-5.003_97d to perl-5.003_97e] CORE LANGUAGE CHANGES Subject: New operator: sysseek() From: Chip Salzenberg Files: doio.c ext/Opcode/Makefile.PL ext/Opcode/Opcode.pm global.sym keywords.pl opcode.pl pod/perldelta.pod pod/perlfunc.pod pp_sys.c t/op/sysio.t toke.c Subject: Allow recursive substitution again From: Chip Salzenberg Files: pod/perldelta.pod pod/perldiag.pod pp_hot.c CORE PORTABILITY Subject: Use size_t for socket size parameters of GNU libc From: Chip Salzenberg Files: doio.c pp_sys.c Subject: Win32 update (four patches) From: Gurusamy Sarathy Files: MANIFEST README.win32 dosish.h ext/SDBM_File/Makefile.PL ext/SDBM_File/sdbm/Makefile.PL ext/SDBM_File/sdbm/sdbm.c ext/SDBM_File/sdbm/sdbm.h lib/ExtUtils/MM_Unix.pm perl.c utils/perlbug.PL utils/perldoc.PL win32/Makefile win32/TEST win32/config.H win32/config.w32 win32/config_h.PL win32/config_sh.PL win32/perllib.c win32/runperl.c win32/win32.c win32/win32io.c win32/win32sck.c DOCUMENTATION Subject: Add CGI to perldelta.pod and improve its description in MANIFEST From: Chip Salzenberg Files: MANIFEST pod/perldelta.pod Subject: Describe probs with majordomo 1.94.1 From: Chip Salzenberg Files: pod/perldelta.pod Subject: Fix description of /\G/g From: Chip Salzenberg Files: pod/perlop.pod Subject: Mention '...' operator in precedence table Date: Sun, 13 Apr 1997 11:24:16 -0600 From: Tom Christiansen Files: pod/perlop.pod private-msgid: 199704131724.LAA23120@jhereg.perl.com OTHER CORE CHANGES Subject: New API function: perl_eval_pv() Date: Mon, 14 Apr 1997 17:13:41 -0400 From: Doug MacEachern Files: perl.c pod/perlcall.pod pod/perldelta.pod pod/perlembed.pod pod/perlguts.pod proto.h private-msgid: 199704142113.RAA06823@postman.osf.org Subject: Fix C< s//whatever/ >, which reuses old pattern From: Chip Salzenberg Files: pp_hot.c regexec.c --- pod/perlembed.pod | 105 ++++++++++++++++++++---------------------------------- 1 file changed, 38 insertions(+), 67 deletions(-) (limited to 'pod/perlembed.pod') diff --git a/pod/perlembed.pod b/pod/perlembed.pod index 9e3fb5250a..79783a7d30 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. @@ -329,28 +321,10 @@ I to create a string: 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 *perl_eval(char *string, int croak_on_error) - { - dSP; - SV *sv = newSVpv(string,0); +from L instead. Example: - PUSHMARK(sp); - perl_eval_sv(sv, G_SCALAR); - SvREFCNT_dec(sv); - - SPAGAIN; - sv = POPs; - PUTBACK; - - if (croak_on_error && SvTRUE(GvSV(errgv))) - croak(SvPV(GvSV(errgv),na)); - - return sv; - } ... - SV *val = perl_eval("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE); + SV *val = perl_eval_pv("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE); printf("%s\n", SvPV(val,na)); ... @@ -359,7 +333,7 @@ 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. @@ -390,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. @@ -406,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)); } @@ -424,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)); @@ -447,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 **/ @@ -990,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 -- cgit v1.2.1