File: tperl.c

package info (click to toggle)
funtools 1.4.4+dfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 16,168 kB
  • ctags: 10,760
  • sloc: ansic: 87,238; sh: 9,727; lex: 4,595; asm: 3,281; ada: 1,681; makefile: 1,458; pascal: 1,089; cpp: 1,001; cs: 879; perl: 161; yacc: 64; sed: 32; csh: 10; tcl: 9
file content (39 lines) | stat: -rw-r--r-- 1,137 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
/*
  http://search.cpan.org/~nwclark/perl-5.8.7/pod/perlembed.pod
  gcc -o tperl tperl.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
 */

#include <EXTERN.h>               /* from the Perl distribution     */
#include <perl.h>                 /* from the Perl distribution     */

static PerlInterpreter *my_perl;  /***    The Perl interpreter    ***/

int main(int argc, char **argv, char **env)
{
  STRLEN n_a;
  char *embedding[] = { "", "-e", "0" };
  
  PERL_SYS_INIT3(&argc,&argv,&env);
  my_perl = perl_alloc();
  perl_construct( my_perl );
  
  perl_parse(my_perl, NULL, 3, embedding, NULL);
  PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
  perl_run(my_perl);
  
  /** Treat $a as an integer **/
  eval_pv("$a = 3; $a **= 2", TRUE);
  printf("a = %d\n", SvIV(get_sv("a", FALSE)));
  
  /** Treat $a as a float **/
  eval_pv("$a = 3.14; $a **= 2", TRUE);
  printf("a = %f\n", SvNV(get_sv("a", FALSE)));
  
  /** Treat $a as a string **/
  eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE);
  printf("a = %s\n", SvPV(get_sv("a", FALSE), n_a));
  
  perl_destruct(my_perl);
  perl_free(my_perl);
  PERL_SYS_TERM();
}