summaryrefslogtreecommitdiff
path: root/reference/C/EXAMPLES/perl2.c
blob: edbde2e3423242d19876958978cfd074de1c69e2 (plain)
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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 <stdio.h>
#include <EXTERN.h>
#include <perl.h>

/********************************************************************/

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);
}