From 7e0f021a9aec35fd8e6725e87e3313b101d26f5e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Sun, 27 Jan 2008 11:37:44 +0100 Subject: Initial import (2.0.2-6) --- reference/C/EXAMPLES/perl2.c | 106 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 reference/C/EXAMPLES/perl2.c (limited to 'reference/C/EXAMPLES/perl2.c') diff --git a/reference/C/EXAMPLES/perl2.c b/reference/C/EXAMPLES/perl2.c new file mode 100644 index 0000000..edbde2e --- /dev/null +++ b/reference/C/EXAMPLES/perl2.c @@ -0,0 +1,106 @@ +/******************************************************************** + * + * Date: 11-May-97 + * + * Purpose: Imbed PERL code in C. + * + * Notes: This code is based on the examples found in the + * Perl man pages. + * + * Compile: To compile this code you will need a starement simular too: + * + * g++ perl2.c -L/usr/lib/perl5/i586-linux/5.003/CORE + * -I/usr/lib/perl5/i586-linux/5.003/CORE + * -lperl + * -lm + * + * This program uses the C++ compiler as there seem to be problems + * With the header files on Linux when using GCC. + * + ********************************************************************/ + +#include +#include +#include + +/********************************************************************/ + +PerlInterpreter *my_perl; + +void PerlInit(char **env); +int PerlEval(char *string); +void PerlEnd (void); + +/********************************************************************/ + +main ( + int argc, + char **argv, + char **env) +{ + unsigned int Length; + + // ... Initalise. + + PerlInit(env); + + // ... Some basic Perl statements + + PerlEval("$a = 3;"); + PerlEval("$a = $a+2;"); + + // ... Extract an integer perl value into the C code. + + printf("a = %d\n", SvIV(perl_get_sv("a", FALSE))); + + PerlEval("$a = $a+2.5;"); + + // ... Extract a float perl value into the C code. + + printf("a = %f5.2\n", SvNV(perl_get_sv("a", FALSE))); + + // ... Play with strings. + + PerlEval("$a = 'rekcaH lreP rehtonA tsuJ';"); + PerlEval("$a = reverse($a); "); + + // ... Extract a string perl value into the C code. + + printf("a = %s\n", SvPV(perl_get_sv("a", FALSE), Length)); + + printf("%d\n", Length); + + // ... Tidy up. + + PerlEnd(); +} + +/********************************************************************/ + +void PerlInit( char **env) +{ + char *Embedding[] = { "", "-e", "sub _eval_ { eval $_[0] }" }; + + my_perl = perl_alloc(); + perl_construct( my_perl ); + + perl_parse(my_perl, NULL, 3, Embedding, env); +} + +/********************************************************************/ + +int PerlEval(char *string) +{ + char *argv[2]; + argv[0] = string; + argv[1] = NULL; + perl_call_argv("_eval_", 0, argv); +} + +/********************************************************************/ + +void PerlEnd(void) +{ + perl_destruct(my_perl); + perl_free(my_perl); +} -- cgit v1.2.3-54-g00ecf