1PERLEMBED(1)           Perl Programmers Reference Guide           PERLEMBED(1)
2
3
4

NAME

6       perlembed - how to embed perl in your C program
7

DESCRIPTION

9   PREAMBLE
10       Do you want to:
11
12       Use C from Perl?
13            Read perlxstut, perlxs, h2xs, perlguts, and perlapi.
14
15       Use a Unix program from Perl?
16            Read about back-quotes and about "system" and "exec" in perlfunc.
17
18       Use Perl from Perl?
19            Read about "do" in perlfunc and "eval" in perlfunc and "require"
20            in perlfunc and "use" in perlfunc.
21
22       Use C from C?
23            Rethink your design.
24
25       Use Perl from C?
26            Read on...
27
28   ROADMAP
29       ·    Compiling your C program
30
31       ·    Adding a Perl interpreter to your C program
32
33       ·    Calling a Perl subroutine from your C program
34
35       ·    Evaluating a Perl statement from your C program
36
37       ·    Performing Perl pattern matches and substitutions from your C
38            program
39
40       ·    Fiddling with the Perl stack from your C program
41
42       ·    Maintaining a persistent interpreter
43
44       ·    Maintaining multiple interpreter instances
45
46       ·    Using Perl modules, which themselves use C libraries, from your C
47            program
48
49       ·    Embedding Perl under Win32
50
51   Compiling your C program
52       If you have trouble compiling the scripts in this documentation, you're
53       not alone.  The cardinal rule: COMPILE THE PROGRAMS IN EXACTLY THE SAME
54       WAY THAT YOUR PERL WAS COMPILED.  (Sorry for yelling.)
55
56       Also, every C program that uses Perl must link in the perl library.
57       What's that, you ask?  Perl is itself written in C; the perl library is
58       the collection of compiled C programs that were used to create your
59       perl executable (/usr/bin/perl or equivalent).  (Corollary: you can't
60       use Perl from your C program unless Perl has been compiled on your
61       machine, or installed properly--that's why you shouldn't blithely copy
62       Perl executables from machine to machine without also copying the lib
63       directory.)
64
65       When you use Perl from C, your C program will--usually--allocate,
66       "run", and deallocate a PerlInterpreter object, which is defined by the
67       perl library.
68
69       If your copy of Perl is recent enough to contain this documentation
70       (version 5.002 or later), then the perl library (and EXTERN.h and
71       perl.h, which you'll also need) will reside in a directory that looks
72       like this:
73
74           /usr/local/lib/perl5/your_architecture_here/CORE
75
76       or perhaps just
77
78           /usr/local/lib/perl5/CORE
79
80       or maybe something like
81
82           /usr/opt/perl5/CORE
83
84       Execute this statement for a hint about where to find CORE:
85
86           perl -MConfig -e 'print $Config{archlib}'
87
88       Here's how you'd compile the example in the next section, "Adding a
89       Perl interpreter to your C program", on my Linux box:
90
91           % gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include
92           -I/usr/local/lib/perl5/i586-linux/5.003/CORE
93           -L/usr/local/lib/perl5/i586-linux/5.003/CORE
94           -o interp interp.c -lperl -lm
95
96       (That's all one line.)  On my DEC Alpha running old 5.003_05, the
97       incantation is a bit different:
98
99           % cc -O2 -Olimit 2900 -DSTANDARD_C -I/usr/local/include
100           -I/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE
101           -L/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE -L/usr/local/lib
102           -D__LANGUAGE_C__ -D_NO_PROTO -o interp interp.c -lperl -lm
103
104       How can you figure out what to add?  Assuming your Perl is post-5.001,
105       execute a "perl -V" command and pay special attention to the "cc" and
106       "ccflags" information.
107
108       You'll have to choose the appropriate compiler (cc, gcc, et al.) for
109       your machine: "perl -MConfig -e 'print $Config{cc}'" will tell you what
110       to use.
111
112       You'll also have to choose the appropriate library directory
113       (/usr/local/lib/...) for your machine.  If your compiler complains that
114       certain functions are undefined, or that it can't locate -lperl, then
115       you need to change the path following the "-L".  If it complains that
116       it can't find EXTERN.h and perl.h, you need to change the path
117       following the "-I".
118
119       You may have to add extra libraries as well.  Which ones?  Perhaps
120       those printed by
121
122          perl -MConfig -e 'print $Config{libs}'
123
124       Provided your perl binary was properly configured and installed the
125       ExtUtils::Embed module will determine all of this information for you:
126
127          % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
128
129       If the ExtUtils::Embed module isn't part of your Perl distribution, you
130       can retrieve it from
131       http://www.perl.com/perl/CPAN/modules/by-module/ExtUtils/ (If this
132       documentation came from your Perl distribution, then you're running
133       5.004 or better and you already have it.)
134
135       The ExtUtils::Embed kit on CPAN also contains all source code for the
136       examples in this document, tests, additional examples and other
137       information you may find useful.
138
139   Adding a Perl interpreter to your C program
140       In a sense, perl (the C program) is a good example of embedding Perl
141       (the language), so I'll demonstrate embedding with miniperlmain.c,
142       included in the source distribution.  Here's a bastardized, non-
143       portable version of miniperlmain.c containing the essentials of
144       embedding:
145
146           #include <EXTERN.h>               /* from the Perl distribution     */
147           #include <perl.h>                 /* from the Perl distribution     */
148
149           static PerlInterpreter *my_perl;  /***    The Perl interpreter    ***/
150
151           int main(int argc, char **argv, char **env)
152           {
153               PERL_SYS_INIT3(&argc,&argv,&env);
154               my_perl = perl_alloc();
155               perl_construct(my_perl);
156               PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
157               perl_parse(my_perl, NULL, argc, argv, (char **)NULL);
158               perl_run(my_perl);
159               perl_destruct(my_perl);
160               perl_free(my_perl);
161               PERL_SYS_TERM();
162           }
163
164       Notice that we don't use the "env" pointer.  Normally handed to
165       "perl_parse" as its final argument, "env" here is replaced by "NULL",
166       which means that the current environment will be used.
167
168       The macros PERL_SYS_INIT3() and PERL_SYS_TERM() provide system-specific
169       tune up of the C runtime environment necessary to run Perl
170       interpreters; they should only be called once regardless of how many
171       interpreters you create or destroy. Call PERL_SYS_INIT3() before you
172       create your first interpreter, and PERL_SYS_TERM() after you free your
173       last interpreter.
174
175       Since PERL_SYS_INIT3() may change "env", it may be more appropriate to
176       provide "env" as an argument to perl_parse().
177
178       Also notice that no matter what arguments you pass to perl_parse(),
179       PERL_SYS_INIT3() must be invoked on the C main() argc, argv and env and
180       only once.
181
182       Now compile this program (I'll call it interp.c) into an executable:
183
184           % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
185
186       After a successful compilation, you'll be able to use interp just like
187       perl itself:
188
189           % interp
190           print "Pretty Good Perl \n";
191           print "10890 - 9801 is ", 10890 - 9801;
192           <CTRL-D>
193           Pretty Good Perl
194           10890 - 9801 is 1089
195
196       or
197
198           % interp -e 'printf("%x", 3735928559)'
199           deadbeef
200
201       You can also read and execute Perl statements from a file while in the
202       midst of your C program, by placing the filename in argv[1] before
203       calling perl_run.
204
205   Calling a Perl subroutine from your C program
206       To call individual Perl subroutines, you can use any of the call_*
207       functions documented in perlcall.  In this example we'll use
208       "call_argv".
209
210       That's shown below, in a program I'll call showtime.c.
211
212           #include <EXTERN.h>
213           #include <perl.h>
214
215           static PerlInterpreter *my_perl;
216
217           int main(int argc, char **argv, char **env)
218           {
219               char *args[] = { NULL };
220               PERL_SYS_INIT3(&argc,&argv,&env);
221               my_perl = perl_alloc();
222               perl_construct(my_perl);
223
224               perl_parse(my_perl, NULL, argc, argv, NULL);
225               PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
226
227               /*** skipping perl_run() ***/
228
229               call_argv("showtime", G_DISCARD | G_NOARGS, args);
230
231               perl_destruct(my_perl);
232               perl_free(my_perl);
233               PERL_SYS_TERM();
234           }
235
236       where showtime is a Perl subroutine that takes no arguments (that's the
237       G_NOARGS) and for which I'll ignore the return value (that's the
238       G_DISCARD).  Those flags, and others, are discussed in perlcall.
239
240       I'll define the showtime subroutine in a file called showtime.pl:
241
242           print "I shan't be printed.";
243
244           sub showtime {
245               print time;
246           }
247
248       Simple enough.  Now compile and run:
249
250           % cc -o showtime showtime.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
251
252           % showtime showtime.pl
253           818284590
254
255       yielding the number of seconds that elapsed between January 1, 1970
256       (the beginning of the Unix epoch), and the moment I began writing this
257       sentence.
258
259       In this particular case we don't have to call perl_run, as we set the
260       PL_exit_flag PERL_EXIT_DESTRUCT_END which executes END blocks in
261       perl_destruct.
262
263       If you want to pass arguments to the Perl subroutine, you can add
264       strings to the "NULL"-terminated "args" list passed to call_argv.  For
265       other data types, or to examine return values, you'll need to
266       manipulate the Perl stack.  That's demonstrated in "Fiddling with the
267       Perl stack from your C program".
268
269   Evaluating a Perl statement from your C program
270       Perl provides two API functions to evaluate pieces of Perl code.  These
271       are "eval_sv" in perlapi and "eval_pv" in perlapi.
272
273       Arguably, these are the only routines you'll ever need to execute
274       snippets of Perl code from within your C program.  Your code can be as
275       long as you wish; it can contain multiple statements; it can employ
276       "use" in perlfunc, "require" in perlfunc, and "do" in perlfunc to
277       include external Perl files.
278
279       eval_pv lets us evaluate individual Perl strings, and then extract
280       variables for coercion into C types.  The following program, string.c,
281       executes three Perl strings, extracting an "int" from the first, a
282       "float" from the second, and a "char *" from the third.
283
284          #include <EXTERN.h>
285          #include <perl.h>
286
287          static PerlInterpreter *my_perl;
288
289          main (int argc, char **argv, char **env)
290          {
291              char *embedding[] = { "", "-e", "0" };
292
293              PERL_SYS_INIT3(&argc,&argv,&env);
294              my_perl = perl_alloc();
295              perl_construct( my_perl );
296
297              perl_parse(my_perl, NULL, 3, embedding, NULL);
298              PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
299              perl_run(my_perl);
300
301              /** Treat $a as an integer **/
302              eval_pv("$a = 3; $a **= 2", TRUE);
303              printf("a = %d\n", SvIV(get_sv("a", 0)));
304
305              /** Treat $a as a float **/
306              eval_pv("$a = 3.14; $a **= 2", TRUE);
307              printf("a = %f\n", SvNV(get_sv("a", 0)));
308
309              /** Treat $a as a string **/
310              eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE);
311              printf("a = %s\n", SvPV_nolen(get_sv("a", 0)));
312
313              perl_destruct(my_perl);
314              perl_free(my_perl);
315              PERL_SYS_TERM();
316          }
317
318       All of those strange functions with sv in their names help convert Perl
319       scalars to C types.  They're described in perlguts and perlapi.
320
321       If you compile and run string.c, you'll see the results of using SvIV()
322       to create an "int", SvNV() to create a "float", and SvPV() to create a
323       string:
324
325          a = 9
326          a = 9.859600
327          a = Just Another Perl Hacker
328
329       In the example above, we've created a global variable to temporarily
330       store the computed value of our eval'ed expression.  It is also
331       possible and in most cases a better strategy to fetch the return value
332       from eval_pv() instead.  Example:
333
334          ...
335          SV *val = eval_pv("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE);
336          printf("%s\n", SvPV_nolen(val));
337          ...
338
339       This way, we avoid namespace pollution by not creating global variables
340       and we've simplified our code as well.
341
342   Performing Perl pattern matches and substitutions from your C program
343       The eval_sv() function lets us evaluate strings of Perl code, so we can
344       define some functions that use it to "specialize" in matches and
345       substitutions: match(), substitute(), and matches().
346
347          I32 match(SV *string, char *pattern);
348
349       Given a string and a pattern (e.g., "m/clasp/" or "/\b\w*\b/", which in
350       your C program might appear as "/\\b\\w*\\b/"), match() returns 1 if
351       the string matches the pattern and 0 otherwise.
352
353          int substitute(SV **string, char *pattern);
354
355       Given a pointer to an "SV" and an "=~" operation (e.g.,
356       "s/bob/robert/g" or "tr[A-Z][a-z]"), substitute() modifies the string
357       within the "SV" as according to the operation, returning the number of
358       substitutions made.
359
360          int matches(SV *string, char *pattern, AV **matches);
361
362       Given an "SV", a pattern, and a pointer to an empty "AV", matches()
363       evaluates "$string =~ $pattern" in a list context, and fills in matches
364       with the array elements, returning the number of matches found.
365
366       Here's a sample program, match.c, that uses all three (long lines have
367       been wrapped here):
368
369        #include <EXTERN.h>
370        #include <perl.h>
371
372        static PerlInterpreter *my_perl;
373
374        /** my_eval_sv(code, error_check)
375        ** kinda like eval_sv(),
376        ** but we pop the return value off the stack
377        **/
378        SV* my_eval_sv(SV *sv, I32 croak_on_error)
379        {
380            dSP;
381            SV* retval;
382
383
384            PUSHMARK(SP);
385            eval_sv(sv, G_SCALAR);
386
387            SPAGAIN;
388            retval = POPs;
389            PUTBACK;
390
391            if (croak_on_error && SvTRUE(ERRSV))
392               croak(SvPVx_nolen(ERRSV));
393
394            return retval;
395        }
396
397        /** match(string, pattern)
398        **
399        ** Used for matches in a scalar context.
400        **
401        ** Returns 1 if the match was successful; 0 otherwise.
402        **/
403
404        I32 match(SV *string, char *pattern)
405        {
406            SV *command = newSV(0), *retval;
407
408            sv_setpvf(command, "my $string = '%s'; $string =~ %s",
409                     SvPV_nolen(string), pattern);
410
411            retval = my_eval_sv(command, TRUE);
412            SvREFCNT_dec(command);
413
414            return SvIV(retval);
415        }
416
417        /** substitute(string, pattern)
418        **
419        ** Used for =~ operations that modify their left-hand side (s/// and tr///)
420        **
421        ** Returns the number of successful matches, and
422        ** modifies the input string if there were any.
423        **/
424
425        I32 substitute(SV **string, char *pattern)
426        {
427            SV *command = newSV(0), *retval;
428
429            sv_setpvf(command, "$string = '%s'; ($string =~ %s)",
430                     SvPV_nolen(*string), pattern);
431
432            retval = my_eval_sv(command, TRUE);
433            SvREFCNT_dec(command);
434
435            *string = get_sv("string", 0);
436            return SvIV(retval);
437        }
438
439        /** matches(string, pattern, matches)
440        **
441        ** Used for matches in a list context.
442        **
443        ** Returns the number of matches,
444        ** and fills in **matches with the matching substrings
445        **/
446
447        I32 matches(SV *string, char *pattern, AV **match_list)
448        {
449            SV *command = newSV(0);
450            I32 num_matches;
451
452            sv_setpvf(command, "my $string = '%s'; @array = ($string =~ %s)",
453                     SvPV_nolen(string), pattern);
454
455            my_eval_sv(command, TRUE);
456            SvREFCNT_dec(command);
457
458            *match_list = get_av("array", 0);
459            num_matches = av_len(*match_list) + 1; /** assume $[ is 0 **/
460
461            return num_matches;
462        }
463
464        main (int argc, char **argv, char **env)
465        {
466            char *embedding[] = { "", "-e", "0" };
467            AV *match_list;
468            I32 num_matches, i;
469            SV *text;
470
471            PERL_SYS_INIT3(&argc,&argv,&env);
472            my_perl = perl_alloc();
473            perl_construct(my_perl);
474            perl_parse(my_perl, NULL, 3, embedding, NULL);
475            PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
476
477            text = newSV(0);
478            sv_setpv(text, "When he is at a convenience store and the "
479               "bill comes to some amount like 76 cents, Maynard is "
480               "aware that there is something he *should* do, something "
481               "that will enable him to get back a quarter, but he has "
482               "no idea *what*.  He fumbles through his red squeezey "
483               "changepurse and gives the boy three extra pennies with "
484               "his dollar, hoping that he might luck into the correct "
485               "amount.  The boy gives him back two of his own pennies "
486               "and then the big shiny quarter that is his prize. "
487               "-RICHH");
488
489            if (match(text, "m/quarter/")) /** Does text contain 'quarter'? **/
490               printf("match: Text contains the word 'quarter'.\n\n");
491            else
492               printf("match: Text doesn't contain the word 'quarter'.\n\n");
493
494            if (match(text, "m/eighth/")) /** Does text contain 'eighth'? **/
495               printf("match: Text contains the word 'eighth'.\n\n");
496            else
497               printf("match: Text doesn't contain the word 'eighth'.\n\n");
498
499            /** Match all occurrences of /wi../ **/
500            num_matches = matches(text, "m/(wi..)/g", &match_list);
501            printf("matches: m/(wi..)/g found %d matches...\n", num_matches);
502
503            for (i = 0; i < num_matches; i++)
504               printf("match: %s\n", SvPV_nolen(*av_fetch(match_list, i, FALSE)));
505            printf("\n");
506
507            /** Remove all vowels from text **/
508            num_matches = substitute(&text, "s/[aeiou]//gi");
509            if (num_matches) {
510               printf("substitute: s/[aeiou]//gi...%d substitutions made.\n",
511                      num_matches);
512               printf("Now text is: %s\n\n", SvPV_nolen(text));
513            }
514
515            /** Attempt a substitution **/
516            if (!substitute(&text, "s/Perl/C/")) {
517               printf("substitute: s/Perl/C...No substitution made.\n\n");
518            }
519
520            SvREFCNT_dec(text);
521            PL_perl_destruct_level = 1;
522            perl_destruct(my_perl);
523            perl_free(my_perl);
524            PERL_SYS_TERM();
525        }
526
527       which produces the output (again, long lines have been wrapped here)
528
529          match: Text contains the word 'quarter'.
530
531          match: Text doesn't contain the word 'eighth'.
532
533          matches: m/(wi..)/g found 2 matches...
534          match: will
535          match: with
536
537          substitute: s/[aeiou]//gi...139 substitutions made.
538          Now text is: Whn h s t  cnvnnc str nd th bll cms t sm mnt lk 76 cnts,
539          Mynrd s wr tht thr s smthng h *shld* d, smthng tht wll nbl hm t gt bck
540          qrtr, bt h hs n d *wht*.  H fmbls thrgh hs rd sqzy chngprs nd gvs th by
541          thr xtr pnns wth hs dllr, hpng tht h mght lck nt th crrct mnt.  Th by gvs
542          hm bck tw f hs wn pnns nd thn th bg shny qrtr tht s hs prz. -RCHH
543
544          substitute: s/Perl/C...No substitution made.
545
546   Fiddling with the Perl stack from your C program
547       When trying to explain stacks, most computer science textbooks mumble
548       something about spring-loaded columns of cafeteria plates: the last
549       thing you pushed on the stack is the first thing you pop off.  That'll
550       do for our purposes: your C program will push some arguments onto "the
551       Perl stack", shut its eyes while some magic happens, and then pop the
552       results--the return value of your Perl subroutine--off the stack.
553
554       First you'll need to know how to convert between C types and Perl
555       types, with newSViv() and sv_setnv() and newAV() and all their friends.
556       They're described in perlguts and perlapi.
557
558       Then you'll need to know how to manipulate the Perl stack.  That's
559       described in perlcall.
560
561       Once you've understood those, embedding Perl in C is easy.
562
563       Because C has no builtin function for integer exponentiation, let's
564       make Perl's ** operator available to it (this is less useful than it
565       sounds, because Perl implements ** with C's pow() function).  First
566       I'll create a stub exponentiation function in power.pl:
567
568           sub expo {
569               my ($a, $b) = @_;
570               return $a ** $b;
571           }
572
573       Now I'll create a C program, power.c, with a function PerlPower() that
574       contains all the perlguts necessary to push the two arguments into
575       expo() and to pop the return value out.  Take a deep breath...
576
577           #include <EXTERN.h>
578           #include <perl.h>
579
580           static PerlInterpreter *my_perl;
581
582           static void
583           PerlPower(int a, int b)
584           {
585             dSP;                            /* initialize stack pointer      */
586             ENTER;                          /* everything created after here */
587             SAVETMPS;                       /* ...is a temporary variable.   */
588             PUSHMARK(SP);                   /* remember the stack pointer    */
589             XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack  */
590             XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack  */
591             PUTBACK;                      /* make local stack pointer global */
592             call_pv("expo", G_SCALAR);      /* call the function             */
593             SPAGAIN;                        /* refresh stack pointer         */
594                                           /* pop the return value from stack */
595             printf ("%d to the %dth power is %d.\n", a, b, POPi);
596             PUTBACK;
597             FREETMPS;                       /* free that return value        */
598             LEAVE;                       /* ...and the XPUSHed "mortal" args.*/
599           }
600
601           int main (int argc, char **argv, char **env)
602           {
603             char *my_argv[] = { "", "power.pl" };
604
605             PERL_SYS_INIT3(&argc,&argv,&env);
606             my_perl = perl_alloc();
607             perl_construct( my_perl );
608
609             perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL);
610             PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
611             perl_run(my_perl);
612
613             PerlPower(3, 4);                      /*** Compute 3 ** 4 ***/
614
615             perl_destruct(my_perl);
616             perl_free(my_perl);
617             PERL_SYS_TERM();
618           }
619
620       Compile and run:
621
622           % cc -o power power.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
623
624           % power
625           3 to the 4th power is 81.
626
627   Maintaining a persistent interpreter
628       When developing interactive and/or potentially long-running
629       applications, it's a good idea to maintain a persistent interpreter
630       rather than allocating and constructing a new interpreter multiple
631       times.  The major reason is speed: since Perl will only be loaded into
632       memory once.
633
634       However, you have to be more cautious with namespace and variable
635       scoping when using a persistent interpreter.  In previous examples
636       we've been using global variables in the default package "main".  We
637       knew exactly what code would be run, and assumed we could avoid
638       variable collisions and outrageous symbol table growth.
639
640       Let's say your application is a server that will occasionally run Perl
641       code from some arbitrary file.  Your server has no way of knowing what
642       code it's going to run.  Very dangerous.
643
644       If the file is pulled in by "perl_parse()", compiled into a newly
645       constructed interpreter, and subsequently cleaned out with
646       "perl_destruct()" afterwards, you're shielded from most namespace
647       troubles.
648
649       One way to avoid namespace collisions in this scenario is to translate
650       the filename into a guaranteed-unique package name, and then compile
651       the code into that package using "eval" in perlfunc.  In the example
652       below, each file will only be compiled once.  Or, the application might
653       choose to clean out the symbol table associated with the file after
654       it's no longer needed.  Using "call_argv" in perlapi, We'll call the
655       subroutine "Embed::Persistent::eval_file" which lives in the file
656       "persistent.pl" and pass the filename and boolean cleanup/cache flag as
657       arguments.
658
659       Note that the process will continue to grow for each file that it uses.
660       In addition, there might be "AUTOLOAD"ed subroutines and other
661       conditions that cause Perl's symbol table to grow.  You might want to
662       add some logic that keeps track of the process size, or restarts itself
663       after a certain number of requests, to ensure that memory consumption
664       is minimized.  You'll also want to scope your variables with "my" in
665       perlfunc whenever possible.
666
667        package Embed::Persistent;
668        #persistent.pl
669
670        use strict;
671        our %Cache;
672        use Symbol qw(delete_package);
673
674        sub valid_package_name {
675            my($string) = @_;
676            $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg;
677            # second pass only for words starting with a digit
678            $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg;
679
680            # Dress it up as a real package name
681            $string =~ s|/|::|g;
682            return "Embed" . $string;
683        }
684
685        sub eval_file {
686            my($filename, $delete) = @_;
687            my $package = valid_package_name($filename);
688            my $mtime = -M $filename;
689            if(defined $Cache{$package}{mtime}
690               &&
691               $Cache{$package}{mtime} <= $mtime)
692            {
693               # we have compiled this subroutine already,
694               # it has not been updated on disk, nothing left to do
695               print STDERR "already compiled $package->handler\n";
696            }
697            else {
698               local *FH;
699               open FH, $filename or die "open '$filename' $!";
700               local($/) = undef;
701               my $sub = <FH>;
702               close FH;
703
704               #wrap the code into a subroutine inside our unique package
705               my $eval = qq{package $package; sub handler { $sub; }};
706               {
707                   # hide our variables within this block
708                   my($filename,$mtime,$package,$sub);
709                   eval $eval;
710               }
711               die $@ if $@;
712
713               #cache it unless we're cleaning out each time
714               $Cache{$package}{mtime} = $mtime unless $delete;
715            }
716
717            eval {$package->handler;};
718            die $@ if $@;
719
720            delete_package($package) if $delete;
721
722            #take a look if you want
723            #print Devel::Symdump->rnew($package)->as_string, $/;
724        }
725
726        1;
727
728        __END__
729
730        /* persistent.c */
731        #include <EXTERN.h>
732        #include <perl.h>
733
734        /* 1 = clean out filename's symbol table after each request, 0 = don't */
735        #ifndef DO_CLEAN
736        #define DO_CLEAN 0
737        #endif
738
739        #define BUFFER_SIZE 1024
740
741        static PerlInterpreter *my_perl = NULL;
742
743        int
744        main(int argc, char **argv, char **env)
745        {
746            char *embedding[] = { "", "persistent.pl" };
747            char *args[] = { "", DO_CLEAN, NULL };
748            char filename[BUFFER_SIZE];
749            int exitstatus = 0;
750
751            PERL_SYS_INIT3(&argc,&argv,&env);
752            if((my_perl = perl_alloc()) == NULL) {
753               fprintf(stderr, "no memory!");
754               exit(1);
755            }
756            perl_construct(my_perl);
757
758            PL_origalen = 1; /* don't let $0 assignment update the proctitle or embedding[0] */
759            exitstatus = perl_parse(my_perl, NULL, 2, embedding, NULL);
760            PL_exit_flags |= PERL_EXIT_DESTRUCT_END;
761            if(!exitstatus) {
762               exitstatus = perl_run(my_perl);
763
764               while(printf("Enter file name: ") &&
765                     fgets(filename, BUFFER_SIZE, stdin)) {
766
767                   filename[strlen(filename)-1] = '\0'; /* strip \n */
768                   /* call the subroutine, passing it the filename as an argument */
769                   args[0] = filename;
770                   call_argv("Embed::Persistent::eval_file",
771                                  G_DISCARD | G_EVAL, args);
772
773                   /* check $@ */
774                   if(SvTRUE(ERRSV))
775                       fprintf(stderr, "eval error: %s\n", SvPV_nolen(ERRSV));
776               }
777            }
778
779            PL_perl_destruct_level = 0;
780            perl_destruct(my_perl);
781            perl_free(my_perl);
782            PERL_SYS_TERM();
783            exit(exitstatus);
784        }
785
786       Now compile:
787
788        % cc -o persistent persistent.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
789
790       Here's an example script file:
791
792        #test.pl
793        my $string = "hello";
794        foo($string);
795
796        sub foo {
797            print "foo says: @_\n";
798        }
799
800       Now run:
801
802        % persistent
803        Enter file name: test.pl
804        foo says: hello
805        Enter file name: test.pl
806        already compiled Embed::test_2epl->handler
807        foo says: hello
808        Enter file name: ^C
809
810   Execution of END blocks
811       Traditionally END blocks have been executed at the end of the perl_run.
812       This causes problems for applications that never call perl_run. Since
813       perl 5.7.2 you can specify "PL_exit_flags |= PERL_EXIT_DESTRUCT_END" to
814       get the new behaviour. This also enables the running of END blocks if
815       the perl_parse fails and "perl_destruct" will return the exit value.
816
817   $0 assignments
818       When a perl script assigns a value to $0 then the perl runtime will try
819       to make this value show up as the program name reported by "ps" by
820       updating the memory pointed to by the argv passed to perl_parse() and
821       also calling API functions like setproctitle() where available.  This
822       behaviour might not be appropriate when embedding perl and can be
823       disabled by assigning the value 1 to the variable "PL_origalen" before
824       perl_parse() is called.
825
826       The persistent.c example above is for instance likely to segfault when
827       $0 is assigned to if the "PL_origalen = 1;" assignment is removed.
828       This because perl will try to write to the read only memory of the
829       "embedding[]" strings.
830
831   Maintaining multiple interpreter instances
832       Some rare applications will need to create more than one interpreter
833       during a session.  Such an application might sporadically decide to
834       release any resources associated with the interpreter.
835
836       The program must take care to ensure that this takes place before the
837       next interpreter is constructed.  By default, when perl is not built
838       with any special options, the global variable "PL_perl_destruct_level"
839       is set to 0, since extra cleaning isn't usually needed when a program
840       only ever creates a single interpreter in its entire lifetime.
841
842       Setting "PL_perl_destruct_level" to 1 makes everything squeaky clean:
843
844        while(1) {
845            ...
846            /* reset global variables here with PL_perl_destruct_level = 1 */
847            PL_perl_destruct_level = 1;
848            perl_construct(my_perl);
849            ...
850            /* clean and reset _everything_ during perl_destruct */
851            PL_perl_destruct_level = 1;
852            perl_destruct(my_perl);
853            perl_free(my_perl);
854            ...
855            /* let's go do it again! */
856        }
857
858       When perl_destruct() is called, the interpreter's syntax parse tree and
859       symbol tables are cleaned up, and global variables are reset.  The
860       second assignment to "PL_perl_destruct_level" is needed because
861       perl_construct resets it to 0.
862
863       Now suppose we have more than one interpreter instance running at the
864       same time.  This is feasible, but only if you used the Configure option
865       "-Dusemultiplicity" or the options "-Dusethreads -Duseithreads" when
866       building perl.  By default, enabling one of these Configure options
867       sets the per-interpreter global variable "PL_perl_destruct_level" to 1,
868       so that thorough cleaning is automatic and interpreter variables are
869       initialized correctly.  Even if you don't intend to run two or more
870       interpreters at the same time, but to run them sequentially, like in
871       the above example, it is recommended to build perl with the
872       "-Dusemultiplicity" option otherwise some interpreter variables may not
873       be initialized correctly between consecutive runs and your application
874       may crash.
875
876       See also "Thread-aware system interfaces" in perlxs.
877
878       Using "-Dusethreads -Duseithreads" rather than "-Dusemultiplicity" is
879       more appropriate if you intend to run multiple interpreters
880       concurrently in different threads, because it enables support for
881       linking in the thread libraries of your system with the interpreter.
882
883       Let's give it a try:
884
885        #include <EXTERN.h>
886        #include <perl.h>
887
888        /* we're going to embed two interpreters */
889        /* we're going to embed two interpreters */
890
891        #define SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)"
892
893        int main(int argc, char **argv, char **env)
894        {
895            PerlInterpreter *one_perl, *two_perl;
896            char *one_args[] = { "one_perl", SAY_HELLO };
897            char *two_args[] = { "two_perl", SAY_HELLO };
898
899            PERL_SYS_INIT3(&argc,&argv,&env);
900            one_perl = perl_alloc();
901            two_perl = perl_alloc();
902
903            PERL_SET_CONTEXT(one_perl);
904            perl_construct(one_perl);
905            PERL_SET_CONTEXT(two_perl);
906            perl_construct(two_perl);
907
908            PERL_SET_CONTEXT(one_perl);
909            perl_parse(one_perl, NULL, 3, one_args, (char **)NULL);
910            PERL_SET_CONTEXT(two_perl);
911            perl_parse(two_perl, NULL, 3, two_args, (char **)NULL);
912
913            PERL_SET_CONTEXT(one_perl);
914            perl_run(one_perl);
915            PERL_SET_CONTEXT(two_perl);
916            perl_run(two_perl);
917
918            PERL_SET_CONTEXT(one_perl);
919            perl_destruct(one_perl);
920            PERL_SET_CONTEXT(two_perl);
921            perl_destruct(two_perl);
922
923            PERL_SET_CONTEXT(one_perl);
924            perl_free(one_perl);
925            PERL_SET_CONTEXT(two_perl);
926            perl_free(two_perl);
927            PERL_SYS_TERM();
928        }
929
930       Note the calls to PERL_SET_CONTEXT().  These are necessary to
931       initialize the global state that tracks which interpreter is the
932       "current" one on the particular process or thread that may be running
933       it.  It should always be used if you have more than one interpreter and
934       are making perl API calls on both interpreters in an interleaved
935       fashion.
936
937       PERL_SET_CONTEXT(interp) should also be called whenever "interp" is
938       used by a thread that did not create it (using either perl_alloc(), or
939       the more esoteric perl_clone()).
940
941       Compile as usual:
942
943        % cc -o multiplicity multiplicity.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
944
945       Run it, Run it:
946
947        % multiplicity
948        Hi, I'm one_perl
949        Hi, I'm two_perl
950
951   Using Perl modules, which themselves use C libraries, from your C program
952       If you've played with the examples above and tried to embed a script
953       that use()s a Perl module (such as Socket) which itself uses a C or C++
954       library, this probably happened:
955
956        Can't load module Socket, dynamic loading not available in this perl.
957         (You may need to build a new perl executable which either supports
958         dynamic loading or has the Socket module statically linked into it.)
959
960       What's wrong?
961
962       Your interpreter doesn't know how to communicate with these extensions
963       on its own.  A little glue will help.  Up until now you've been calling
964       perl_parse(), handing it NULL for the second argument:
965
966        perl_parse(my_perl, NULL, argc, my_argv, NULL);
967
968       That's where the glue code can be inserted to create the initial
969       contact between Perl and linked C/C++ routines.  Let's take a look some
970       pieces of perlmain.c to see how Perl does this:
971
972        static void xs_init (pTHX);
973
974        EXTERN_C void boot_DynaLoader (pTHX_ CV* cv);
975        EXTERN_C void boot_Socket (pTHX_ CV* cv);
976
977
978        EXTERN_C void
979        xs_init(pTHX)
980        {
981               char *file = __FILE__;
982               /* DynaLoader is a special case */
983               newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file);
984               newXS("Socket::bootstrap", boot_Socket, file);
985        }
986
987       Simply put: for each extension linked with your Perl executable
988       (determined during its initial configuration on your computer or when
989       adding a new extension), a Perl subroutine is created to incorporate
990       the extension's routines.  Normally, that subroutine is named
991       Module::bootstrap() and is invoked when you say use Module.  In turn,
992       this hooks into an XSUB, boot_Module, which creates a Perl counterpart
993       for each of the extension's XSUBs.  Don't worry about this part; leave
994       that to the xsubpp and extension authors.  If your extension is
995       dynamically loaded, DynaLoader creates Module::bootstrap() for you on
996       the fly.  In fact, if you have a working DynaLoader then there is
997       rarely any need to link in any other extensions statically.
998
999       Once you have this code, slap it into the second argument of
1000       perl_parse():
1001
1002        perl_parse(my_perl, xs_init, argc, my_argv, NULL);
1003
1004       Then compile:
1005
1006        % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts`
1007
1008        % interp
1009          use Socket;
1010          use SomeDynamicallyLoadedModule;
1011
1012          print "Now I can use extensions!\n"'
1013
1014       ExtUtils::Embed can also automate writing the xs_init glue code.
1015
1016        % perl -MExtUtils::Embed -e xsinit -- -o perlxsi.c
1017        % cc -c perlxsi.c `perl -MExtUtils::Embed -e ccopts`
1018        % cc -c interp.c  `perl -MExtUtils::Embed -e ccopts`
1019        % cc -o interp perlxsi.o interp.o `perl -MExtUtils::Embed -e ldopts`
1020
1021       Consult perlxs, perlguts, and perlapi for more details.
1022

Embedding Perl under Win32

1024       In general, all of the source code shown here should work unmodified
1025       under Windows.
1026
1027       However, there are some caveats about the command-line examples shown.
1028       For starters, backticks won't work under the Win32 native command
1029       shell.  The ExtUtils::Embed kit on CPAN ships with a script called
1030       genmake, which generates a simple makefile to build a program from a
1031       single C source file.  It can be used like this:
1032
1033        C:\ExtUtils-Embed\eg> perl genmake interp.c
1034        C:\ExtUtils-Embed\eg> nmake
1035        C:\ExtUtils-Embed\eg> interp -e "print qq{I'm embedded in Win32!\n}"
1036
1037       You may wish to use a more robust environment such as the Microsoft
1038       Developer Studio.  In this case, run this to generate perlxsi.c:
1039
1040        perl -MExtUtils::Embed -e xsinit
1041
1042       Create a new project and Insert -> Files into Project: perlxsi.c,
1043       perl.lib, and your own source files, e.g. interp.c.  Typically you'll
1044       find perl.lib in C:\perl\lib\CORE, if not, you should see the CORE
1045       directory relative to "perl -V:archlib".  The studio will also need
1046       this path so it knows where to find Perl include files.  This path can
1047       be added via the Tools -> Options -> Directories menu.  Finally, select
1048       Build -> Build interp.exe and you're ready to go.
1049

Hiding Perl_

1051       If you completely hide the short forms forms of the Perl public API,
1052       add -DPERL_NO_SHORT_NAMES to the compilation flags.  This means that
1053       for example instead of writing
1054
1055           warn("%d bottles of beer on the wall", bottlecount);
1056
1057       you will have to write the explicit full form
1058
1059           Perl_warn(aTHX_ "%d bottles of beer on the wall", bottlecount);
1060
1061       (See "Background and PERL_IMPLICIT_CONTEXT for the explanation of the
1062       "aTHX_"." in perlguts )  Hiding the short forms is very useful for
1063       avoiding all sorts of nasty (C preprocessor or otherwise) conflicts
1064       with other software packages (Perl defines about 2400 APIs with these
1065       short names, take or leave few hundred, so there certainly is room for
1066       conflict.)
1067

MORAL

1069       You can sometimes write faster code in C, but you can always write code
1070       faster in Perl.  Because you can use each from the other, combine them
1071       as you wish.
1072

AUTHOR

1074       Jon Orwant <orwant@media.mit.edu> and Doug MacEachern
1075       <dougm@covalent.net>, with small contributions from Tim Bunce, Tom
1076       Christiansen, Guy Decoux, Hallvard Furuseth, Dov Grobgeld, and Ilya
1077       Zakharevich.
1078
1079       Doug MacEachern has an article on embedding in Volume 1, Issue 4 of The
1080       Perl Journal ( http://www.tpj.com/ ).  Doug is also the developer of
1081       the most widely-used Perl embedding: the mod_perl system
1082       (perl.apache.org), which embeds Perl in the Apache web server.  Oracle,
1083       Binary Evolution, ActiveState, and Ben Sugars's nsapi_perl have used
1084       this model for Oracle, Netscape and Internet Information Server Perl
1085       plugins.
1086
1088       Copyright (C) 1995, 1996, 1997, 1998 Doug MacEachern and Jon Orwant.
1089       All Rights Reserved.
1090
1091       Permission is granted to make and distribute verbatim copies of this
1092       documentation provided the copyright notice and this permission notice
1093       are preserved on all copies.
1094
1095       Permission is granted to copy and distribute modified versions of this
1096       documentation under the conditions for verbatim copying, provided also
1097       that they are marked clearly as modified versions, that the authors'
1098       names and title are unchanged (though subtitles and additional authors'
1099       names may be added), and that the entire resulting derived work is
1100       distributed under the terms of a permission notice identical to this
1101       one.
1102
1103       Permission is granted to copy and distribute translations of this
1104       documentation into another language, under the above conditions for
1105       modified versions.
1106
1107
1108
1109perl v5.10.1                      2009-04-22                      PERLEMBED(1)
Impressum