1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlembed - how to embed perl in your C program 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gate=head2 PREAMBLE 8*0Sstevel@tonic-gate 9*0Sstevel@tonic-gateDo you want to: 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gate=over 5 12*0Sstevel@tonic-gate 13*0Sstevel@tonic-gate=item B<Use C from Perl?> 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gateRead L<perlxstut>, L<perlxs>, L<h2xs>, L<perlguts>, and L<perlapi>. 16*0Sstevel@tonic-gate 17*0Sstevel@tonic-gate=item B<Use a Unix program from Perl?> 18*0Sstevel@tonic-gate 19*0Sstevel@tonic-gateRead about back-quotes and about C<system> and C<exec> in L<perlfunc>. 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gate=item B<Use Perl from Perl?> 22*0Sstevel@tonic-gate 23*0Sstevel@tonic-gateRead about L<perlfunc/do> and L<perlfunc/eval> and L<perlfunc/require> 24*0Sstevel@tonic-gateand L<perlfunc/use>. 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate=item B<Use C from C?> 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gateRethink your design. 29*0Sstevel@tonic-gate 30*0Sstevel@tonic-gate=item B<Use Perl from C?> 31*0Sstevel@tonic-gate 32*0Sstevel@tonic-gateRead on... 33*0Sstevel@tonic-gate 34*0Sstevel@tonic-gate=back 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate=head2 ROADMAP 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gate=over 5 39*0Sstevel@tonic-gate 40*0Sstevel@tonic-gate=item * 41*0Sstevel@tonic-gate 42*0Sstevel@tonic-gateCompiling your C program 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gate=item * 45*0Sstevel@tonic-gate 46*0Sstevel@tonic-gateAdding a Perl interpreter to your C program 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gate=item * 49*0Sstevel@tonic-gate 50*0Sstevel@tonic-gateCalling a Perl subroutine from your C program 51*0Sstevel@tonic-gate 52*0Sstevel@tonic-gate=item * 53*0Sstevel@tonic-gate 54*0Sstevel@tonic-gateEvaluating a Perl statement from your C program 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gate=item * 57*0Sstevel@tonic-gate 58*0Sstevel@tonic-gatePerforming Perl pattern matches and substitutions from your C program 59*0Sstevel@tonic-gate 60*0Sstevel@tonic-gate=item * 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gateFiddling with the Perl stack from your C program 63*0Sstevel@tonic-gate 64*0Sstevel@tonic-gate=item * 65*0Sstevel@tonic-gate 66*0Sstevel@tonic-gateMaintaining a persistent interpreter 67*0Sstevel@tonic-gate 68*0Sstevel@tonic-gate=item * 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gateMaintaining multiple interpreter instances 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate=item * 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gateUsing Perl modules, which themselves use C libraries, from your C program 75*0Sstevel@tonic-gate 76*0Sstevel@tonic-gate=item * 77*0Sstevel@tonic-gate 78*0Sstevel@tonic-gateEmbedding Perl under Win32 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gate=back 81*0Sstevel@tonic-gate 82*0Sstevel@tonic-gate=head2 Compiling your C program 83*0Sstevel@tonic-gate 84*0Sstevel@tonic-gateIf you have trouble compiling the scripts in this documentation, 85*0Sstevel@tonic-gateyou're not alone. The cardinal rule: COMPILE THE PROGRAMS IN EXACTLY 86*0Sstevel@tonic-gateTHE SAME WAY THAT YOUR PERL WAS COMPILED. (Sorry for yelling.) 87*0Sstevel@tonic-gate 88*0Sstevel@tonic-gateAlso, every C program that uses Perl must link in the I<perl library>. 89*0Sstevel@tonic-gateWhat's that, you ask? Perl is itself written in C; the perl library 90*0Sstevel@tonic-gateis the collection of compiled C programs that were used to create your 91*0Sstevel@tonic-gateperl executable (I</usr/bin/perl> or equivalent). (Corollary: you 92*0Sstevel@tonic-gatecan't use Perl from your C program unless Perl has been compiled on 93*0Sstevel@tonic-gateyour machine, or installed properly--that's why you shouldn't blithely 94*0Sstevel@tonic-gatecopy Perl executables from machine to machine without also copying the 95*0Sstevel@tonic-gateI<lib> directory.) 96*0Sstevel@tonic-gate 97*0Sstevel@tonic-gateWhen you use Perl from C, your C program will--usually--allocate, 98*0Sstevel@tonic-gate"run", and deallocate a I<PerlInterpreter> object, which is defined by 99*0Sstevel@tonic-gatethe perl library. 100*0Sstevel@tonic-gate 101*0Sstevel@tonic-gateIf your copy of Perl is recent enough to contain this documentation 102*0Sstevel@tonic-gate(version 5.002 or later), then the perl library (and I<EXTERN.h> and 103*0Sstevel@tonic-gateI<perl.h>, which you'll also need) will reside in a directory 104*0Sstevel@tonic-gatethat looks like this: 105*0Sstevel@tonic-gate 106*0Sstevel@tonic-gate /usr/local/lib/perl5/your_architecture_here/CORE 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gateor perhaps just 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gate /usr/local/lib/perl5/CORE 111*0Sstevel@tonic-gate 112*0Sstevel@tonic-gateor maybe something like 113*0Sstevel@tonic-gate 114*0Sstevel@tonic-gate /usr/opt/perl5/CORE 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gateExecute this statement for a hint about where to find CORE: 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate perl -MConfig -e 'print $Config{archlib}' 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gateHere's how you'd compile the example in the next section, 121*0Sstevel@tonic-gateL<Adding a Perl interpreter to your C program>, on my Linux box: 122*0Sstevel@tonic-gate 123*0Sstevel@tonic-gate % gcc -O2 -Dbool=char -DHAS_BOOL -I/usr/local/include 124*0Sstevel@tonic-gate -I/usr/local/lib/perl5/i586-linux/5.003/CORE 125*0Sstevel@tonic-gate -L/usr/local/lib/perl5/i586-linux/5.003/CORE 126*0Sstevel@tonic-gate -o interp interp.c -lperl -lm 127*0Sstevel@tonic-gate 128*0Sstevel@tonic-gate(That's all one line.) On my DEC Alpha running old 5.003_05, the 129*0Sstevel@tonic-gateincantation is a bit different: 130*0Sstevel@tonic-gate 131*0Sstevel@tonic-gate % cc -O2 -Olimit 2900 -DSTANDARD_C -I/usr/local/include 132*0Sstevel@tonic-gate -I/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE 133*0Sstevel@tonic-gate -L/usr/local/lib/perl5/alpha-dec_osf/5.00305/CORE -L/usr/local/lib 134*0Sstevel@tonic-gate -D__LANGUAGE_C__ -D_NO_PROTO -o interp interp.c -lperl -lm 135*0Sstevel@tonic-gate 136*0Sstevel@tonic-gateHow can you figure out what to add? Assuming your Perl is post-5.001, 137*0Sstevel@tonic-gateexecute a C<perl -V> command and pay special attention to the "cc" and 138*0Sstevel@tonic-gate"ccflags" information. 139*0Sstevel@tonic-gate 140*0Sstevel@tonic-gateYou'll have to choose the appropriate compiler (I<cc>, I<gcc>, et al.) for 141*0Sstevel@tonic-gateyour machine: C<perl -MConfig -e 'print $Config{cc}'> will tell you what 142*0Sstevel@tonic-gateto use. 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gateYou'll also have to choose the appropriate library directory 145*0Sstevel@tonic-gate(I</usr/local/lib/...>) for your machine. If your compiler complains 146*0Sstevel@tonic-gatethat certain functions are undefined, or that it can't locate 147*0Sstevel@tonic-gateI<-lperl>, then you need to change the path following the C<-L>. If it 148*0Sstevel@tonic-gatecomplains that it can't find I<EXTERN.h> and I<perl.h>, you need to 149*0Sstevel@tonic-gatechange the path following the C<-I>. 150*0Sstevel@tonic-gate 151*0Sstevel@tonic-gateYou may have to add extra libraries as well. Which ones? 152*0Sstevel@tonic-gatePerhaps those printed by 153*0Sstevel@tonic-gate 154*0Sstevel@tonic-gate perl -MConfig -e 'print $Config{libs}' 155*0Sstevel@tonic-gate 156*0Sstevel@tonic-gateProvided your perl binary was properly configured and installed the 157*0Sstevel@tonic-gateB<ExtUtils::Embed> module will determine all of this information for 158*0Sstevel@tonic-gateyou: 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gate % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 161*0Sstevel@tonic-gate 162*0Sstevel@tonic-gateIf the B<ExtUtils::Embed> module isn't part of your Perl distribution, 163*0Sstevel@tonic-gateyou can retrieve it from 164*0Sstevel@tonic-gatehttp://www.perl.com/perl/CPAN/modules/by-module/ExtUtils/ 165*0Sstevel@tonic-gate(If this documentation came from your Perl distribution, then you're 166*0Sstevel@tonic-gaterunning 5.004 or better and you already have it.) 167*0Sstevel@tonic-gate 168*0Sstevel@tonic-gateThe B<ExtUtils::Embed> kit on CPAN also contains all source code for 169*0Sstevel@tonic-gatethe examples in this document, tests, additional examples and other 170*0Sstevel@tonic-gateinformation you may find useful. 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gate=head2 Adding a Perl interpreter to your C program 173*0Sstevel@tonic-gate 174*0Sstevel@tonic-gateIn a sense, perl (the C program) is a good example of embedding Perl 175*0Sstevel@tonic-gate(the language), so I'll demonstrate embedding with I<miniperlmain.c>, 176*0Sstevel@tonic-gateincluded in the source distribution. Here's a bastardized, nonportable 177*0Sstevel@tonic-gateversion of I<miniperlmain.c> containing the essentials of embedding: 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate #include <EXTERN.h> /* from the Perl distribution */ 180*0Sstevel@tonic-gate #include <perl.h> /* from the Perl distribution */ 181*0Sstevel@tonic-gate 182*0Sstevel@tonic-gate static PerlInterpreter *my_perl; /*** The Perl interpreter ***/ 183*0Sstevel@tonic-gate 184*0Sstevel@tonic-gate int main(int argc, char **argv, char **env) 185*0Sstevel@tonic-gate { 186*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 187*0Sstevel@tonic-gate my_perl = perl_alloc(); 188*0Sstevel@tonic-gate perl_construct(my_perl); 189*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 190*0Sstevel@tonic-gate perl_parse(my_perl, NULL, argc, argv, (char **)NULL); 191*0Sstevel@tonic-gate perl_run(my_perl); 192*0Sstevel@tonic-gate perl_destruct(my_perl); 193*0Sstevel@tonic-gate perl_free(my_perl); 194*0Sstevel@tonic-gate PERL_SYS_TERM(); 195*0Sstevel@tonic-gate } 196*0Sstevel@tonic-gate 197*0Sstevel@tonic-gateNotice that we don't use the C<env> pointer. Normally handed to 198*0Sstevel@tonic-gateC<perl_parse> as its final argument, C<env> here is replaced by 199*0Sstevel@tonic-gateC<NULL>, which means that the current environment will be used. The macros 200*0Sstevel@tonic-gatePERL_SYS_INIT3() and PERL_SYS_TERM() provide system-specific tune up 201*0Sstevel@tonic-gateof the C runtime environment necessary to run Perl interpreters; since 202*0Sstevel@tonic-gatePERL_SYS_INIT3() may change C<env>, it may be more appropriate to provide 203*0Sstevel@tonic-gateC<env> as an argument to perl_parse(). 204*0Sstevel@tonic-gate 205*0Sstevel@tonic-gateNow compile this program (I'll call it I<interp.c>) into an executable: 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gateAfter a successful compilation, you'll be able to use I<interp> just 210*0Sstevel@tonic-gatelike perl itself: 211*0Sstevel@tonic-gate 212*0Sstevel@tonic-gate % interp 213*0Sstevel@tonic-gate print "Pretty Good Perl \n"; 214*0Sstevel@tonic-gate print "10890 - 9801 is ", 10890 - 9801; 215*0Sstevel@tonic-gate <CTRL-D> 216*0Sstevel@tonic-gate Pretty Good Perl 217*0Sstevel@tonic-gate 10890 - 9801 is 1089 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gateor 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gate % interp -e 'printf("%x", 3735928559)' 222*0Sstevel@tonic-gate deadbeef 223*0Sstevel@tonic-gate 224*0Sstevel@tonic-gateYou can also read and execute Perl statements from a file while in the 225*0Sstevel@tonic-gatemidst of your C program, by placing the filename in I<argv[1]> before 226*0Sstevel@tonic-gatecalling I<perl_run>. 227*0Sstevel@tonic-gate 228*0Sstevel@tonic-gate=head2 Calling a Perl subroutine from your C program 229*0Sstevel@tonic-gate 230*0Sstevel@tonic-gateTo call individual Perl subroutines, you can use any of the B<call_*> 231*0Sstevel@tonic-gatefunctions documented in L<perlcall>. 232*0Sstevel@tonic-gateIn this example we'll use C<call_argv>. 233*0Sstevel@tonic-gate 234*0Sstevel@tonic-gateThat's shown below, in a program I'll call I<showtime.c>. 235*0Sstevel@tonic-gate 236*0Sstevel@tonic-gate #include <EXTERN.h> 237*0Sstevel@tonic-gate #include <perl.h> 238*0Sstevel@tonic-gate 239*0Sstevel@tonic-gate static PerlInterpreter *my_perl; 240*0Sstevel@tonic-gate 241*0Sstevel@tonic-gate int main(int argc, char **argv, char **env) 242*0Sstevel@tonic-gate { 243*0Sstevel@tonic-gate char *args[] = { NULL }; 244*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 245*0Sstevel@tonic-gate my_perl = perl_alloc(); 246*0Sstevel@tonic-gate perl_construct(my_perl); 247*0Sstevel@tonic-gate 248*0Sstevel@tonic-gate perl_parse(my_perl, NULL, argc, argv, NULL); 249*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 250*0Sstevel@tonic-gate 251*0Sstevel@tonic-gate /*** skipping perl_run() ***/ 252*0Sstevel@tonic-gate 253*0Sstevel@tonic-gate call_argv("showtime", G_DISCARD | G_NOARGS, args); 254*0Sstevel@tonic-gate 255*0Sstevel@tonic-gate perl_destruct(my_perl); 256*0Sstevel@tonic-gate perl_free(my_perl); 257*0Sstevel@tonic-gate PERL_SYS_TERM(); 258*0Sstevel@tonic-gate } 259*0Sstevel@tonic-gate 260*0Sstevel@tonic-gatewhere I<showtime> is a Perl subroutine that takes no arguments (that's the 261*0Sstevel@tonic-gateI<G_NOARGS>) and for which I'll ignore the return value (that's the 262*0Sstevel@tonic-gateI<G_DISCARD>). Those flags, and others, are discussed in L<perlcall>. 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gateI'll define the I<showtime> subroutine in a file called I<showtime.pl>: 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gate print "I shan't be printed."; 267*0Sstevel@tonic-gate 268*0Sstevel@tonic-gate sub showtime { 269*0Sstevel@tonic-gate print time; 270*0Sstevel@tonic-gate } 271*0Sstevel@tonic-gate 272*0Sstevel@tonic-gateSimple enough. Now compile and run: 273*0Sstevel@tonic-gate 274*0Sstevel@tonic-gate % cc -o showtime showtime.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 275*0Sstevel@tonic-gate 276*0Sstevel@tonic-gate % showtime showtime.pl 277*0Sstevel@tonic-gate 818284590 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gateyielding the number of seconds that elapsed between January 1, 1970 280*0Sstevel@tonic-gate(the beginning of the Unix epoch), and the moment I began writing this 281*0Sstevel@tonic-gatesentence. 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gateIn this particular case we don't have to call I<perl_run>, as we set 284*0Sstevel@tonic-gatethe PL_exit_flag PERL_EXIT_DESTRUCT_END which executes END blocks in 285*0Sstevel@tonic-gateperl_destruct. 286*0Sstevel@tonic-gate 287*0Sstevel@tonic-gateIf you want to pass arguments to the Perl subroutine, you can add 288*0Sstevel@tonic-gatestrings to the C<NULL>-terminated C<args> list passed to 289*0Sstevel@tonic-gateI<call_argv>. For other data types, or to examine return values, 290*0Sstevel@tonic-gateyou'll need to manipulate the Perl stack. That's demonstrated in 291*0Sstevel@tonic-gateL<Fiddling with the Perl stack from your C program>. 292*0Sstevel@tonic-gate 293*0Sstevel@tonic-gate=head2 Evaluating a Perl statement from your C program 294*0Sstevel@tonic-gate 295*0Sstevel@tonic-gatePerl provides two API functions to evaluate pieces of Perl code. 296*0Sstevel@tonic-gateThese are L<perlapi/eval_sv> and L<perlapi/eval_pv>. 297*0Sstevel@tonic-gate 298*0Sstevel@tonic-gateArguably, these are the only routines you'll ever need to execute 299*0Sstevel@tonic-gatesnippets of Perl code from within your C program. Your code can be as 300*0Sstevel@tonic-gatelong as you wish; it can contain multiple statements; it can employ 301*0Sstevel@tonic-gateL<perlfunc/use>, L<perlfunc/require>, and L<perlfunc/do> to 302*0Sstevel@tonic-gateinclude external Perl files. 303*0Sstevel@tonic-gate 304*0Sstevel@tonic-gateI<eval_pv> lets us evaluate individual Perl strings, and then 305*0Sstevel@tonic-gateextract variables for coercion into C types. The following program, 306*0Sstevel@tonic-gateI<string.c>, executes three Perl strings, extracting an C<int> from 307*0Sstevel@tonic-gatethe first, a C<float> from the second, and a C<char *> from the third. 308*0Sstevel@tonic-gate 309*0Sstevel@tonic-gate #include <EXTERN.h> 310*0Sstevel@tonic-gate #include <perl.h> 311*0Sstevel@tonic-gate 312*0Sstevel@tonic-gate static PerlInterpreter *my_perl; 313*0Sstevel@tonic-gate 314*0Sstevel@tonic-gate main (int argc, char **argv, char **env) 315*0Sstevel@tonic-gate { 316*0Sstevel@tonic-gate STRLEN n_a; 317*0Sstevel@tonic-gate char *embedding[] = { "", "-e", "0" }; 318*0Sstevel@tonic-gate 319*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 320*0Sstevel@tonic-gate my_perl = perl_alloc(); 321*0Sstevel@tonic-gate perl_construct( my_perl ); 322*0Sstevel@tonic-gate 323*0Sstevel@tonic-gate perl_parse(my_perl, NULL, 3, embedding, NULL); 324*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 325*0Sstevel@tonic-gate perl_run(my_perl); 326*0Sstevel@tonic-gate 327*0Sstevel@tonic-gate /** Treat $a as an integer **/ 328*0Sstevel@tonic-gate eval_pv("$a = 3; $a **= 2", TRUE); 329*0Sstevel@tonic-gate printf("a = %d\n", SvIV(get_sv("a", FALSE))); 330*0Sstevel@tonic-gate 331*0Sstevel@tonic-gate /** Treat $a as a float **/ 332*0Sstevel@tonic-gate eval_pv("$a = 3.14; $a **= 2", TRUE); 333*0Sstevel@tonic-gate printf("a = %f\n", SvNV(get_sv("a", FALSE))); 334*0Sstevel@tonic-gate 335*0Sstevel@tonic-gate /** Treat $a as a string **/ 336*0Sstevel@tonic-gate eval_pv("$a = 'rekcaH lreP rehtonA tsuJ'; $a = reverse($a);", TRUE); 337*0Sstevel@tonic-gate printf("a = %s\n", SvPV(get_sv("a", FALSE), n_a)); 338*0Sstevel@tonic-gate 339*0Sstevel@tonic-gate perl_destruct(my_perl); 340*0Sstevel@tonic-gate perl_free(my_perl); 341*0Sstevel@tonic-gate PERL_SYS_TERM(); 342*0Sstevel@tonic-gate } 343*0Sstevel@tonic-gate 344*0Sstevel@tonic-gateAll of those strange functions with I<sv> in their names help convert Perl scalars to C types. They're described in L<perlguts> and L<perlapi>. 345*0Sstevel@tonic-gate 346*0Sstevel@tonic-gateIf you compile and run I<string.c>, you'll see the results of using 347*0Sstevel@tonic-gateI<SvIV()> to create an C<int>, I<SvNV()> to create a C<float>, and 348*0Sstevel@tonic-gateI<SvPV()> to create a string: 349*0Sstevel@tonic-gate 350*0Sstevel@tonic-gate a = 9 351*0Sstevel@tonic-gate a = 9.859600 352*0Sstevel@tonic-gate a = Just Another Perl Hacker 353*0Sstevel@tonic-gate 354*0Sstevel@tonic-gateIn the example above, we've created a global variable to temporarily 355*0Sstevel@tonic-gatestore the computed value of our eval'd expression. It is also 356*0Sstevel@tonic-gatepossible and in most cases a better strategy to fetch the return value 357*0Sstevel@tonic-gatefrom I<eval_pv()> instead. Example: 358*0Sstevel@tonic-gate 359*0Sstevel@tonic-gate ... 360*0Sstevel@tonic-gate STRLEN n_a; 361*0Sstevel@tonic-gate SV *val = eval_pv("reverse 'rekcaH lreP rehtonA tsuJ'", TRUE); 362*0Sstevel@tonic-gate printf("%s\n", SvPV(val,n_a)); 363*0Sstevel@tonic-gate ... 364*0Sstevel@tonic-gate 365*0Sstevel@tonic-gateThis way, we avoid namespace pollution by not creating global 366*0Sstevel@tonic-gatevariables and we've simplified our code as well. 367*0Sstevel@tonic-gate 368*0Sstevel@tonic-gate=head2 Performing Perl pattern matches and substitutions from your C program 369*0Sstevel@tonic-gate 370*0Sstevel@tonic-gateThe I<eval_sv()> function lets us evaluate strings of Perl code, so we can 371*0Sstevel@tonic-gatedefine some functions that use it to "specialize" in matches and 372*0Sstevel@tonic-gatesubstitutions: I<match()>, I<substitute()>, and I<matches()>. 373*0Sstevel@tonic-gate 374*0Sstevel@tonic-gate I32 match(SV *string, char *pattern); 375*0Sstevel@tonic-gate 376*0Sstevel@tonic-gateGiven a string and a pattern (e.g., C<m/clasp/> or C</\b\w*\b/>, which 377*0Sstevel@tonic-gatein your C program might appear as "/\\b\\w*\\b/"), match() 378*0Sstevel@tonic-gatereturns 1 if the string matches the pattern and 0 otherwise. 379*0Sstevel@tonic-gate 380*0Sstevel@tonic-gate int substitute(SV **string, char *pattern); 381*0Sstevel@tonic-gate 382*0Sstevel@tonic-gateGiven a pointer to an C<SV> and an C<=~> operation (e.g., 383*0Sstevel@tonic-gateC<s/bob/robert/g> or C<tr[A-Z][a-z]>), substitute() modifies the string 384*0Sstevel@tonic-gatewithin the C<SV> as according to the operation, returning the number of substitutions 385*0Sstevel@tonic-gatemade. 386*0Sstevel@tonic-gate 387*0Sstevel@tonic-gate int matches(SV *string, char *pattern, AV **matches); 388*0Sstevel@tonic-gate 389*0Sstevel@tonic-gateGiven an C<SV>, a pattern, and a pointer to an empty C<AV>, 390*0Sstevel@tonic-gatematches() evaluates C<$string =~ $pattern> in a list context, and 391*0Sstevel@tonic-gatefills in I<matches> with the array elements, returning the number of matches found. 392*0Sstevel@tonic-gate 393*0Sstevel@tonic-gateHere's a sample program, I<match.c>, that uses all three (long lines have 394*0Sstevel@tonic-gatebeen wrapped here): 395*0Sstevel@tonic-gate 396*0Sstevel@tonic-gate #include <EXTERN.h> 397*0Sstevel@tonic-gate #include <perl.h> 398*0Sstevel@tonic-gate 399*0Sstevel@tonic-gate static PerlInterpreter *my_perl; 400*0Sstevel@tonic-gate 401*0Sstevel@tonic-gate /** my_eval_sv(code, error_check) 402*0Sstevel@tonic-gate ** kinda like eval_sv(), 403*0Sstevel@tonic-gate ** but we pop the return value off the stack 404*0Sstevel@tonic-gate **/ 405*0Sstevel@tonic-gate SV* my_eval_sv(SV *sv, I32 croak_on_error) 406*0Sstevel@tonic-gate { 407*0Sstevel@tonic-gate dSP; 408*0Sstevel@tonic-gate SV* retval; 409*0Sstevel@tonic-gate STRLEN n_a; 410*0Sstevel@tonic-gate 411*0Sstevel@tonic-gate PUSHMARK(SP); 412*0Sstevel@tonic-gate eval_sv(sv, G_SCALAR); 413*0Sstevel@tonic-gate 414*0Sstevel@tonic-gate SPAGAIN; 415*0Sstevel@tonic-gate retval = POPs; 416*0Sstevel@tonic-gate PUTBACK; 417*0Sstevel@tonic-gate 418*0Sstevel@tonic-gate if (croak_on_error && SvTRUE(ERRSV)) 419*0Sstevel@tonic-gate croak(SvPVx(ERRSV, n_a)); 420*0Sstevel@tonic-gate 421*0Sstevel@tonic-gate return retval; 422*0Sstevel@tonic-gate } 423*0Sstevel@tonic-gate 424*0Sstevel@tonic-gate /** match(string, pattern) 425*0Sstevel@tonic-gate ** 426*0Sstevel@tonic-gate ** Used for matches in a scalar context. 427*0Sstevel@tonic-gate ** 428*0Sstevel@tonic-gate ** Returns 1 if the match was successful; 0 otherwise. 429*0Sstevel@tonic-gate **/ 430*0Sstevel@tonic-gate 431*0Sstevel@tonic-gate I32 match(SV *string, char *pattern) 432*0Sstevel@tonic-gate { 433*0Sstevel@tonic-gate SV *command = NEWSV(1099, 0), *retval; 434*0Sstevel@tonic-gate STRLEN n_a; 435*0Sstevel@tonic-gate 436*0Sstevel@tonic-gate sv_setpvf(command, "my $string = '%s'; $string =~ %s", 437*0Sstevel@tonic-gate SvPV(string,n_a), pattern); 438*0Sstevel@tonic-gate 439*0Sstevel@tonic-gate retval = my_eval_sv(command, TRUE); 440*0Sstevel@tonic-gate SvREFCNT_dec(command); 441*0Sstevel@tonic-gate 442*0Sstevel@tonic-gate return SvIV(retval); 443*0Sstevel@tonic-gate } 444*0Sstevel@tonic-gate 445*0Sstevel@tonic-gate /** substitute(string, pattern) 446*0Sstevel@tonic-gate ** 447*0Sstevel@tonic-gate ** Used for =~ operations that modify their left-hand side (s/// and tr///) 448*0Sstevel@tonic-gate ** 449*0Sstevel@tonic-gate ** Returns the number of successful matches, and 450*0Sstevel@tonic-gate ** modifies the input string if there were any. 451*0Sstevel@tonic-gate **/ 452*0Sstevel@tonic-gate 453*0Sstevel@tonic-gate I32 substitute(SV **string, char *pattern) 454*0Sstevel@tonic-gate { 455*0Sstevel@tonic-gate SV *command = NEWSV(1099, 0), *retval; 456*0Sstevel@tonic-gate STRLEN n_a; 457*0Sstevel@tonic-gate 458*0Sstevel@tonic-gate sv_setpvf(command, "$string = '%s'; ($string =~ %s)", 459*0Sstevel@tonic-gate SvPV(*string,n_a), pattern); 460*0Sstevel@tonic-gate 461*0Sstevel@tonic-gate retval = my_eval_sv(command, TRUE); 462*0Sstevel@tonic-gate SvREFCNT_dec(command); 463*0Sstevel@tonic-gate 464*0Sstevel@tonic-gate *string = get_sv("string", FALSE); 465*0Sstevel@tonic-gate return SvIV(retval); 466*0Sstevel@tonic-gate } 467*0Sstevel@tonic-gate 468*0Sstevel@tonic-gate /** matches(string, pattern, matches) 469*0Sstevel@tonic-gate ** 470*0Sstevel@tonic-gate ** Used for matches in a list context. 471*0Sstevel@tonic-gate ** 472*0Sstevel@tonic-gate ** Returns the number of matches, 473*0Sstevel@tonic-gate ** and fills in **matches with the matching substrings 474*0Sstevel@tonic-gate **/ 475*0Sstevel@tonic-gate 476*0Sstevel@tonic-gate I32 matches(SV *string, char *pattern, AV **match_list) 477*0Sstevel@tonic-gate { 478*0Sstevel@tonic-gate SV *command = NEWSV(1099, 0); 479*0Sstevel@tonic-gate I32 num_matches; 480*0Sstevel@tonic-gate STRLEN n_a; 481*0Sstevel@tonic-gate 482*0Sstevel@tonic-gate sv_setpvf(command, "my $string = '%s'; @array = ($string =~ %s)", 483*0Sstevel@tonic-gate SvPV(string,n_a), pattern); 484*0Sstevel@tonic-gate 485*0Sstevel@tonic-gate my_eval_sv(command, TRUE); 486*0Sstevel@tonic-gate SvREFCNT_dec(command); 487*0Sstevel@tonic-gate 488*0Sstevel@tonic-gate *match_list = get_av("array", FALSE); 489*0Sstevel@tonic-gate num_matches = av_len(*match_list) + 1; /** assume $[ is 0 **/ 490*0Sstevel@tonic-gate 491*0Sstevel@tonic-gate return num_matches; 492*0Sstevel@tonic-gate } 493*0Sstevel@tonic-gate 494*0Sstevel@tonic-gate main (int argc, char **argv, char **env) 495*0Sstevel@tonic-gate { 496*0Sstevel@tonic-gate char *embedding[] = { "", "-e", "0" }; 497*0Sstevel@tonic-gate AV *match_list; 498*0Sstevel@tonic-gate I32 num_matches, i; 499*0Sstevel@tonic-gate SV *text; 500*0Sstevel@tonic-gate STRLEN n_a; 501*0Sstevel@tonic-gate 502*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 503*0Sstevel@tonic-gate my_perl = perl_alloc(); 504*0Sstevel@tonic-gate perl_construct(my_perl); 505*0Sstevel@tonic-gate perl_parse(my_perl, NULL, 3, embedding, NULL); 506*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 507*0Sstevel@tonic-gate 508*0Sstevel@tonic-gate text = NEWSV(1099,0); 509*0Sstevel@tonic-gate sv_setpv(text, "When he is at a convenience store and the " 510*0Sstevel@tonic-gate "bill comes to some amount like 76 cents, Maynard is " 511*0Sstevel@tonic-gate "aware that there is something he *should* do, something " 512*0Sstevel@tonic-gate "that will enable him to get back a quarter, but he has " 513*0Sstevel@tonic-gate "no idea *what*. He fumbles through his red squeezey " 514*0Sstevel@tonic-gate "changepurse and gives the boy three extra pennies with " 515*0Sstevel@tonic-gate "his dollar, hoping that he might luck into the correct " 516*0Sstevel@tonic-gate "amount. The boy gives him back two of his own pennies " 517*0Sstevel@tonic-gate "and then the big shiny quarter that is his prize. " 518*0Sstevel@tonic-gate "-RICHH"); 519*0Sstevel@tonic-gate 520*0Sstevel@tonic-gate if (match(text, "m/quarter/")) /** Does text contain 'quarter'? **/ 521*0Sstevel@tonic-gate printf("match: Text contains the word 'quarter'.\n\n"); 522*0Sstevel@tonic-gate else 523*0Sstevel@tonic-gate printf("match: Text doesn't contain the word 'quarter'.\n\n"); 524*0Sstevel@tonic-gate 525*0Sstevel@tonic-gate if (match(text, "m/eighth/")) /** Does text contain 'eighth'? **/ 526*0Sstevel@tonic-gate printf("match: Text contains the word 'eighth'.\n\n"); 527*0Sstevel@tonic-gate else 528*0Sstevel@tonic-gate printf("match: Text doesn't contain the word 'eighth'.\n\n"); 529*0Sstevel@tonic-gate 530*0Sstevel@tonic-gate /** Match all occurrences of /wi../ **/ 531*0Sstevel@tonic-gate num_matches = matches(text, "m/(wi..)/g", &match_list); 532*0Sstevel@tonic-gate printf("matches: m/(wi..)/g found %d matches...\n", num_matches); 533*0Sstevel@tonic-gate 534*0Sstevel@tonic-gate for (i = 0; i < num_matches; i++) 535*0Sstevel@tonic-gate printf("match: %s\n", SvPV(*av_fetch(match_list, i, FALSE),n_a)); 536*0Sstevel@tonic-gate printf("\n"); 537*0Sstevel@tonic-gate 538*0Sstevel@tonic-gate /** Remove all vowels from text **/ 539*0Sstevel@tonic-gate num_matches = substitute(&text, "s/[aeiou]//gi"); 540*0Sstevel@tonic-gate if (num_matches) { 541*0Sstevel@tonic-gate printf("substitute: s/[aeiou]//gi...%d substitutions made.\n", 542*0Sstevel@tonic-gate num_matches); 543*0Sstevel@tonic-gate printf("Now text is: %s\n\n", SvPV(text,n_a)); 544*0Sstevel@tonic-gate } 545*0Sstevel@tonic-gate 546*0Sstevel@tonic-gate /** Attempt a substitution **/ 547*0Sstevel@tonic-gate if (!substitute(&text, "s/Perl/C/")) { 548*0Sstevel@tonic-gate printf("substitute: s/Perl/C...No substitution made.\n\n"); 549*0Sstevel@tonic-gate } 550*0Sstevel@tonic-gate 551*0Sstevel@tonic-gate SvREFCNT_dec(text); 552*0Sstevel@tonic-gate PL_perl_destruct_level = 1; 553*0Sstevel@tonic-gate perl_destruct(my_perl); 554*0Sstevel@tonic-gate perl_free(my_perl); 555*0Sstevel@tonic-gate PERL_SYS_TERM(); 556*0Sstevel@tonic-gate } 557*0Sstevel@tonic-gate 558*0Sstevel@tonic-gatewhich produces the output (again, long lines have been wrapped here) 559*0Sstevel@tonic-gate 560*0Sstevel@tonic-gate match: Text contains the word 'quarter'. 561*0Sstevel@tonic-gate 562*0Sstevel@tonic-gate match: Text doesn't contain the word 'eighth'. 563*0Sstevel@tonic-gate 564*0Sstevel@tonic-gate matches: m/(wi..)/g found 2 matches... 565*0Sstevel@tonic-gate match: will 566*0Sstevel@tonic-gate match: with 567*0Sstevel@tonic-gate 568*0Sstevel@tonic-gate substitute: s/[aeiou]//gi...139 substitutions made. 569*0Sstevel@tonic-gate Now text is: Whn h s t cnvnnc str nd th bll cms t sm mnt lk 76 cnts, 570*0Sstevel@tonic-gate Mynrd s wr tht thr s smthng h *shld* d, smthng tht wll nbl hm t gt bck 571*0Sstevel@tonic-gate qrtr, bt h hs n d *wht*. H fmbls thrgh hs rd sqzy chngprs nd gvs th by 572*0Sstevel@tonic-gate thr xtr pnns wth hs dllr, hpng tht h mght lck nt th crrct mnt. Th by gvs 573*0Sstevel@tonic-gate hm bck tw f hs wn pnns nd thn th bg shny qrtr tht s hs prz. -RCHH 574*0Sstevel@tonic-gate 575*0Sstevel@tonic-gate substitute: s/Perl/C...No substitution made. 576*0Sstevel@tonic-gate 577*0Sstevel@tonic-gate=head2 Fiddling with the Perl stack from your C program 578*0Sstevel@tonic-gate 579*0Sstevel@tonic-gateWhen trying to explain stacks, most computer science textbooks mumble 580*0Sstevel@tonic-gatesomething about spring-loaded columns of cafeteria plates: the last 581*0Sstevel@tonic-gatething you pushed on the stack is the first thing you pop off. That'll 582*0Sstevel@tonic-gatedo for our purposes: your C program will push some arguments onto "the Perl 583*0Sstevel@tonic-gatestack", shut its eyes while some magic happens, and then pop the 584*0Sstevel@tonic-gateresults--the return value of your Perl subroutine--off the stack. 585*0Sstevel@tonic-gate 586*0Sstevel@tonic-gateFirst you'll need to know how to convert between C types and Perl 587*0Sstevel@tonic-gatetypes, with newSViv() and sv_setnv() and newAV() and all their 588*0Sstevel@tonic-gatefriends. They're described in L<perlguts> and L<perlapi>. 589*0Sstevel@tonic-gate 590*0Sstevel@tonic-gateThen you'll need to know how to manipulate the Perl stack. That's 591*0Sstevel@tonic-gatedescribed in L<perlcall>. 592*0Sstevel@tonic-gate 593*0Sstevel@tonic-gateOnce you've understood those, embedding Perl in C is easy. 594*0Sstevel@tonic-gate 595*0Sstevel@tonic-gateBecause C has no builtin function for integer exponentiation, let's 596*0Sstevel@tonic-gatemake Perl's ** operator available to it (this is less useful than it 597*0Sstevel@tonic-gatesounds, because Perl implements ** with C's I<pow()> function). First 598*0Sstevel@tonic-gateI'll create a stub exponentiation function in I<power.pl>: 599*0Sstevel@tonic-gate 600*0Sstevel@tonic-gate sub expo { 601*0Sstevel@tonic-gate my ($a, $b) = @_; 602*0Sstevel@tonic-gate return $a ** $b; 603*0Sstevel@tonic-gate } 604*0Sstevel@tonic-gate 605*0Sstevel@tonic-gateNow I'll create a C program, I<power.c>, with a function 606*0Sstevel@tonic-gateI<PerlPower()> that contains all the perlguts necessary to push the 607*0Sstevel@tonic-gatetwo arguments into I<expo()> and to pop the return value out. Take a 608*0Sstevel@tonic-gatedeep breath... 609*0Sstevel@tonic-gate 610*0Sstevel@tonic-gate #include <EXTERN.h> 611*0Sstevel@tonic-gate #include <perl.h> 612*0Sstevel@tonic-gate 613*0Sstevel@tonic-gate static PerlInterpreter *my_perl; 614*0Sstevel@tonic-gate 615*0Sstevel@tonic-gate static void 616*0Sstevel@tonic-gate PerlPower(int a, int b) 617*0Sstevel@tonic-gate { 618*0Sstevel@tonic-gate dSP; /* initialize stack pointer */ 619*0Sstevel@tonic-gate ENTER; /* everything created after here */ 620*0Sstevel@tonic-gate SAVETMPS; /* ...is a temporary variable. */ 621*0Sstevel@tonic-gate PUSHMARK(SP); /* remember the stack pointer */ 622*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(a))); /* push the base onto the stack */ 623*0Sstevel@tonic-gate XPUSHs(sv_2mortal(newSViv(b))); /* push the exponent onto stack */ 624*0Sstevel@tonic-gate PUTBACK; /* make local stack pointer global */ 625*0Sstevel@tonic-gate call_pv("expo", G_SCALAR); /* call the function */ 626*0Sstevel@tonic-gate SPAGAIN; /* refresh stack pointer */ 627*0Sstevel@tonic-gate /* pop the return value from stack */ 628*0Sstevel@tonic-gate printf ("%d to the %dth power is %d.\n", a, b, POPi); 629*0Sstevel@tonic-gate PUTBACK; 630*0Sstevel@tonic-gate FREETMPS; /* free that return value */ 631*0Sstevel@tonic-gate LEAVE; /* ...and the XPUSHed "mortal" args.*/ 632*0Sstevel@tonic-gate } 633*0Sstevel@tonic-gate 634*0Sstevel@tonic-gate int main (int argc, char **argv, char **env) 635*0Sstevel@tonic-gate { 636*0Sstevel@tonic-gate char *my_argv[] = { "", "power.pl" }; 637*0Sstevel@tonic-gate 638*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 639*0Sstevel@tonic-gate my_perl = perl_alloc(); 640*0Sstevel@tonic-gate perl_construct( my_perl ); 641*0Sstevel@tonic-gate 642*0Sstevel@tonic-gate perl_parse(my_perl, NULL, 2, my_argv, (char **)NULL); 643*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 644*0Sstevel@tonic-gate perl_run(my_perl); 645*0Sstevel@tonic-gate 646*0Sstevel@tonic-gate PerlPower(3, 4); /*** Compute 3 ** 4 ***/ 647*0Sstevel@tonic-gate 648*0Sstevel@tonic-gate perl_destruct(my_perl); 649*0Sstevel@tonic-gate perl_free(my_perl); 650*0Sstevel@tonic-gate PERL_SYS_TERM(); 651*0Sstevel@tonic-gate } 652*0Sstevel@tonic-gate 653*0Sstevel@tonic-gate 654*0Sstevel@tonic-gate 655*0Sstevel@tonic-gateCompile and run: 656*0Sstevel@tonic-gate 657*0Sstevel@tonic-gate % cc -o power power.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 658*0Sstevel@tonic-gate 659*0Sstevel@tonic-gate % power 660*0Sstevel@tonic-gate 3 to the 4th power is 81. 661*0Sstevel@tonic-gate 662*0Sstevel@tonic-gate=head2 Maintaining a persistent interpreter 663*0Sstevel@tonic-gate 664*0Sstevel@tonic-gateWhen developing interactive and/or potentially long-running 665*0Sstevel@tonic-gateapplications, it's a good idea to maintain a persistent interpreter 666*0Sstevel@tonic-gaterather than allocating and constructing a new interpreter multiple 667*0Sstevel@tonic-gatetimes. The major reason is speed: since Perl will only be loaded into 668*0Sstevel@tonic-gatememory once. 669*0Sstevel@tonic-gate 670*0Sstevel@tonic-gateHowever, you have to be more cautious with namespace and variable 671*0Sstevel@tonic-gatescoping when using a persistent interpreter. In previous examples 672*0Sstevel@tonic-gatewe've been using global variables in the default package C<main>. We 673*0Sstevel@tonic-gateknew exactly what code would be run, and assumed we could avoid 674*0Sstevel@tonic-gatevariable collisions and outrageous symbol table growth. 675*0Sstevel@tonic-gate 676*0Sstevel@tonic-gateLet's say your application is a server that will occasionally run Perl 677*0Sstevel@tonic-gatecode from some arbitrary file. Your server has no way of knowing what 678*0Sstevel@tonic-gatecode it's going to run. Very dangerous. 679*0Sstevel@tonic-gate 680*0Sstevel@tonic-gateIf the file is pulled in by C<perl_parse()>, compiled into a newly 681*0Sstevel@tonic-gateconstructed interpreter, and subsequently cleaned out with 682*0Sstevel@tonic-gateC<perl_destruct()> afterwards, you're shielded from most namespace 683*0Sstevel@tonic-gatetroubles. 684*0Sstevel@tonic-gate 685*0Sstevel@tonic-gateOne way to avoid namespace collisions in this scenario is to translate 686*0Sstevel@tonic-gatethe filename into a guaranteed-unique package name, and then compile 687*0Sstevel@tonic-gatethe code into that package using L<perlfunc/eval>. In the example 688*0Sstevel@tonic-gatebelow, each file will only be compiled once. Or, the application 689*0Sstevel@tonic-gatemight choose to clean out the symbol table associated with the file 690*0Sstevel@tonic-gateafter it's no longer needed. Using L<perlapi/call_argv>, We'll 691*0Sstevel@tonic-gatecall the subroutine C<Embed::Persistent::eval_file> which lives in the 692*0Sstevel@tonic-gatefile C<persistent.pl> and pass the filename and boolean cleanup/cache 693*0Sstevel@tonic-gateflag as arguments. 694*0Sstevel@tonic-gate 695*0Sstevel@tonic-gateNote that the process will continue to grow for each file that it 696*0Sstevel@tonic-gateuses. In addition, there might be C<AUTOLOAD>ed subroutines and other 697*0Sstevel@tonic-gateconditions that cause Perl's symbol table to grow. You might want to 698*0Sstevel@tonic-gateadd some logic that keeps track of the process size, or restarts 699*0Sstevel@tonic-gateitself after a certain number of requests, to ensure that memory 700*0Sstevel@tonic-gateconsumption is minimized. You'll also want to scope your variables 701*0Sstevel@tonic-gatewith L<perlfunc/my> whenever possible. 702*0Sstevel@tonic-gate 703*0Sstevel@tonic-gate 704*0Sstevel@tonic-gate package Embed::Persistent; 705*0Sstevel@tonic-gate #persistent.pl 706*0Sstevel@tonic-gate 707*0Sstevel@tonic-gate use strict; 708*0Sstevel@tonic-gate our %Cache; 709*0Sstevel@tonic-gate use Symbol qw(delete_package); 710*0Sstevel@tonic-gate 711*0Sstevel@tonic-gate sub valid_package_name { 712*0Sstevel@tonic-gate my($string) = @_; 713*0Sstevel@tonic-gate $string =~ s/([^A-Za-z0-9\/])/sprintf("_%2x",unpack("C",$1))/eg; 714*0Sstevel@tonic-gate # second pass only for words starting with a digit 715*0Sstevel@tonic-gate $string =~ s|/(\d)|sprintf("/_%2x",unpack("C",$1))|eg; 716*0Sstevel@tonic-gate 717*0Sstevel@tonic-gate # Dress it up as a real package name 718*0Sstevel@tonic-gate $string =~ s|/|::|g; 719*0Sstevel@tonic-gate return "Embed" . $string; 720*0Sstevel@tonic-gate } 721*0Sstevel@tonic-gate 722*0Sstevel@tonic-gate sub eval_file { 723*0Sstevel@tonic-gate my($filename, $delete) = @_; 724*0Sstevel@tonic-gate my $package = valid_package_name($filename); 725*0Sstevel@tonic-gate my $mtime = -M $filename; 726*0Sstevel@tonic-gate if(defined $Cache{$package}{mtime} 727*0Sstevel@tonic-gate && 728*0Sstevel@tonic-gate $Cache{$package}{mtime} <= $mtime) 729*0Sstevel@tonic-gate { 730*0Sstevel@tonic-gate # we have compiled this subroutine already, 731*0Sstevel@tonic-gate # it has not been updated on disk, nothing left to do 732*0Sstevel@tonic-gate print STDERR "already compiled $package->handler\n"; 733*0Sstevel@tonic-gate } 734*0Sstevel@tonic-gate else { 735*0Sstevel@tonic-gate local *FH; 736*0Sstevel@tonic-gate open FH, $filename or die "open '$filename' $!"; 737*0Sstevel@tonic-gate local($/) = undef; 738*0Sstevel@tonic-gate my $sub = <FH>; 739*0Sstevel@tonic-gate close FH; 740*0Sstevel@tonic-gate 741*0Sstevel@tonic-gate #wrap the code into a subroutine inside our unique package 742*0Sstevel@tonic-gate my $eval = qq{package $package; sub handler { $sub; }}; 743*0Sstevel@tonic-gate { 744*0Sstevel@tonic-gate # hide our variables within this block 745*0Sstevel@tonic-gate my($filename,$mtime,$package,$sub); 746*0Sstevel@tonic-gate eval $eval; 747*0Sstevel@tonic-gate } 748*0Sstevel@tonic-gate die $@ if $@; 749*0Sstevel@tonic-gate 750*0Sstevel@tonic-gate #cache it unless we're cleaning out each time 751*0Sstevel@tonic-gate $Cache{$package}{mtime} = $mtime unless $delete; 752*0Sstevel@tonic-gate } 753*0Sstevel@tonic-gate 754*0Sstevel@tonic-gate eval {$package->handler;}; 755*0Sstevel@tonic-gate die $@ if $@; 756*0Sstevel@tonic-gate 757*0Sstevel@tonic-gate delete_package($package) if $delete; 758*0Sstevel@tonic-gate 759*0Sstevel@tonic-gate #take a look if you want 760*0Sstevel@tonic-gate #print Devel::Symdump->rnew($package)->as_string, $/; 761*0Sstevel@tonic-gate } 762*0Sstevel@tonic-gate 763*0Sstevel@tonic-gate 1; 764*0Sstevel@tonic-gate 765*0Sstevel@tonic-gate __END__ 766*0Sstevel@tonic-gate 767*0Sstevel@tonic-gate /* persistent.c */ 768*0Sstevel@tonic-gate #include <EXTERN.h> 769*0Sstevel@tonic-gate #include <perl.h> 770*0Sstevel@tonic-gate 771*0Sstevel@tonic-gate /* 1 = clean out filename's symbol table after each request, 0 = don't */ 772*0Sstevel@tonic-gate #ifndef DO_CLEAN 773*0Sstevel@tonic-gate #define DO_CLEAN 0 774*0Sstevel@tonic-gate #endif 775*0Sstevel@tonic-gate 776*0Sstevel@tonic-gate #define BUFFER_SIZE 1024 777*0Sstevel@tonic-gate 778*0Sstevel@tonic-gate static PerlInterpreter *my_perl = NULL; 779*0Sstevel@tonic-gate 780*0Sstevel@tonic-gate int 781*0Sstevel@tonic-gate main(int argc, char **argv, char **env) 782*0Sstevel@tonic-gate { 783*0Sstevel@tonic-gate char *embedding[] = { "", "persistent.pl" }; 784*0Sstevel@tonic-gate char *args[] = { "", DO_CLEAN, NULL }; 785*0Sstevel@tonic-gate char filename[BUFFER_SIZE]; 786*0Sstevel@tonic-gate int exitstatus = 0; 787*0Sstevel@tonic-gate STRLEN n_a; 788*0Sstevel@tonic-gate 789*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 790*0Sstevel@tonic-gate if((my_perl = perl_alloc()) == NULL) { 791*0Sstevel@tonic-gate fprintf(stderr, "no memory!"); 792*0Sstevel@tonic-gate exit(1); 793*0Sstevel@tonic-gate } 794*0Sstevel@tonic-gate perl_construct(my_perl); 795*0Sstevel@tonic-gate 796*0Sstevel@tonic-gate exitstatus = perl_parse(my_perl, NULL, 2, embedding, NULL); 797*0Sstevel@tonic-gate PL_exit_flags |= PERL_EXIT_DESTRUCT_END; 798*0Sstevel@tonic-gate if(!exitstatus) { 799*0Sstevel@tonic-gate exitstatus = perl_run(my_perl); 800*0Sstevel@tonic-gate 801*0Sstevel@tonic-gate while(printf("Enter file name: ") && 802*0Sstevel@tonic-gate fgets(filename, BUFFER_SIZE, stdin)) { 803*0Sstevel@tonic-gate 804*0Sstevel@tonic-gate filename[strlen(filename)-1] = '\0'; /* strip \n */ 805*0Sstevel@tonic-gate /* call the subroutine, passing it the filename as an argument */ 806*0Sstevel@tonic-gate args[0] = filename; 807*0Sstevel@tonic-gate call_argv("Embed::Persistent::eval_file", 808*0Sstevel@tonic-gate G_DISCARD | G_EVAL, args); 809*0Sstevel@tonic-gate 810*0Sstevel@tonic-gate /* check $@ */ 811*0Sstevel@tonic-gate if(SvTRUE(ERRSV)) 812*0Sstevel@tonic-gate fprintf(stderr, "eval error: %s\n", SvPV(ERRSV,n_a)); 813*0Sstevel@tonic-gate } 814*0Sstevel@tonic-gate } 815*0Sstevel@tonic-gate 816*0Sstevel@tonic-gate PL_perl_destruct_level = 0; 817*0Sstevel@tonic-gate perl_destruct(my_perl); 818*0Sstevel@tonic-gate perl_free(my_perl); 819*0Sstevel@tonic-gate PERL_SYS_TERM(); 820*0Sstevel@tonic-gate exit(exitstatus); 821*0Sstevel@tonic-gate } 822*0Sstevel@tonic-gate 823*0Sstevel@tonic-gateNow compile: 824*0Sstevel@tonic-gate 825*0Sstevel@tonic-gate % cc -o persistent persistent.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 826*0Sstevel@tonic-gate 827*0Sstevel@tonic-gateHere's an example script file: 828*0Sstevel@tonic-gate 829*0Sstevel@tonic-gate #test.pl 830*0Sstevel@tonic-gate my $string = "hello"; 831*0Sstevel@tonic-gate foo($string); 832*0Sstevel@tonic-gate 833*0Sstevel@tonic-gate sub foo { 834*0Sstevel@tonic-gate print "foo says: @_\n"; 835*0Sstevel@tonic-gate } 836*0Sstevel@tonic-gate 837*0Sstevel@tonic-gateNow run: 838*0Sstevel@tonic-gate 839*0Sstevel@tonic-gate % persistent 840*0Sstevel@tonic-gate Enter file name: test.pl 841*0Sstevel@tonic-gate foo says: hello 842*0Sstevel@tonic-gate Enter file name: test.pl 843*0Sstevel@tonic-gate already compiled Embed::test_2epl->handler 844*0Sstevel@tonic-gate foo says: hello 845*0Sstevel@tonic-gate Enter file name: ^C 846*0Sstevel@tonic-gate 847*0Sstevel@tonic-gate=head2 Execution of END blocks 848*0Sstevel@tonic-gate 849*0Sstevel@tonic-gateTraditionally END blocks have been executed at the end of the perl_run. 850*0Sstevel@tonic-gateThis causes problems for applications that never call perl_run. Since 851*0Sstevel@tonic-gateperl 5.7.2 you can specify C<PL_exit_flags |= PERL_EXIT_DESTRUCT_END> 852*0Sstevel@tonic-gateto get the new behaviour. This also enables the running of END blocks if 853*0Sstevel@tonic-gatethe perl_parse fails and C<perl_destruct> will return the exit value. 854*0Sstevel@tonic-gate 855*0Sstevel@tonic-gate=head2 Maintaining multiple interpreter instances 856*0Sstevel@tonic-gate 857*0Sstevel@tonic-gateSome rare applications will need to create more than one interpreter 858*0Sstevel@tonic-gateduring a session. Such an application might sporadically decide to 859*0Sstevel@tonic-gaterelease any resources associated with the interpreter. 860*0Sstevel@tonic-gate 861*0Sstevel@tonic-gateThe program must take care to ensure that this takes place I<before> 862*0Sstevel@tonic-gatethe next interpreter is constructed. By default, when perl is not 863*0Sstevel@tonic-gatebuilt with any special options, the global variable 864*0Sstevel@tonic-gateC<PL_perl_destruct_level> is set to C<0>, since extra cleaning isn't 865*0Sstevel@tonic-gateusually needed when a program only ever creates a single interpreter 866*0Sstevel@tonic-gatein its entire lifetime. 867*0Sstevel@tonic-gate 868*0Sstevel@tonic-gateSetting C<PL_perl_destruct_level> to C<1> makes everything squeaky clean: 869*0Sstevel@tonic-gate 870*0Sstevel@tonic-gate while(1) { 871*0Sstevel@tonic-gate ... 872*0Sstevel@tonic-gate /* reset global variables here with PL_perl_destruct_level = 1 */ 873*0Sstevel@tonic-gate PL_perl_destruct_level = 1; 874*0Sstevel@tonic-gate perl_construct(my_perl); 875*0Sstevel@tonic-gate ... 876*0Sstevel@tonic-gate /* clean and reset _everything_ during perl_destruct */ 877*0Sstevel@tonic-gate PL_perl_destruct_level = 1; 878*0Sstevel@tonic-gate perl_destruct(my_perl); 879*0Sstevel@tonic-gate perl_free(my_perl); 880*0Sstevel@tonic-gate ... 881*0Sstevel@tonic-gate /* let's go do it again! */ 882*0Sstevel@tonic-gate } 883*0Sstevel@tonic-gate 884*0Sstevel@tonic-gateWhen I<perl_destruct()> is called, the interpreter's syntax parse tree 885*0Sstevel@tonic-gateand symbol tables are cleaned up, and global variables are reset. The 886*0Sstevel@tonic-gatesecond assignment to C<PL_perl_destruct_level> is needed because 887*0Sstevel@tonic-gateperl_construct resets it to C<0>. 888*0Sstevel@tonic-gate 889*0Sstevel@tonic-gateNow suppose we have more than one interpreter instance running at the 890*0Sstevel@tonic-gatesame time. This is feasible, but only if you used the Configure option 891*0Sstevel@tonic-gateC<-Dusemultiplicity> or the options C<-Dusethreads -Duseithreads> when 892*0Sstevel@tonic-gatebuilding perl. By default, enabling one of these Configure options 893*0Sstevel@tonic-gatesets the per-interpreter global variable C<PL_perl_destruct_level> to 894*0Sstevel@tonic-gateC<1>, so that thorough cleaning is automatic and interpreter variables 895*0Sstevel@tonic-gateare initialized correctly. Even if you don't intend to run two or 896*0Sstevel@tonic-gatemore interpreters at the same time, but to run them sequentially, like 897*0Sstevel@tonic-gatein the above example, it is recommended to build perl with the 898*0Sstevel@tonic-gateC<-Dusemultiplicity> option otherwise some interpreter variables may 899*0Sstevel@tonic-gatenot be initialized correctly between consecutive runs and your 900*0Sstevel@tonic-gateapplication may crash. 901*0Sstevel@tonic-gate 902*0Sstevel@tonic-gateUsing C<-Dusethreads -Duseithreads> rather than C<-Dusemultiplicity> 903*0Sstevel@tonic-gateis more appropriate if you intend to run multiple interpreters 904*0Sstevel@tonic-gateconcurrently in different threads, because it enables support for 905*0Sstevel@tonic-gatelinking in the thread libraries of your system with the interpreter. 906*0Sstevel@tonic-gate 907*0Sstevel@tonic-gateLet's give it a try: 908*0Sstevel@tonic-gate 909*0Sstevel@tonic-gate 910*0Sstevel@tonic-gate #include <EXTERN.h> 911*0Sstevel@tonic-gate #include <perl.h> 912*0Sstevel@tonic-gate 913*0Sstevel@tonic-gate /* we're going to embed two interpreters */ 914*0Sstevel@tonic-gate /* we're going to embed two interpreters */ 915*0Sstevel@tonic-gate 916*0Sstevel@tonic-gate #define SAY_HELLO "-e", "print qq(Hi, I'm $^X\n)" 917*0Sstevel@tonic-gate 918*0Sstevel@tonic-gate int main(int argc, char **argv, char **env) 919*0Sstevel@tonic-gate { 920*0Sstevel@tonic-gate PerlInterpreter *one_perl, *two_perl; 921*0Sstevel@tonic-gate char *one_args[] = { "one_perl", SAY_HELLO }; 922*0Sstevel@tonic-gate char *two_args[] = { "two_perl", SAY_HELLO }; 923*0Sstevel@tonic-gate 924*0Sstevel@tonic-gate PERL_SYS_INIT3(&argc,&argv,&env); 925*0Sstevel@tonic-gate one_perl = perl_alloc(); 926*0Sstevel@tonic-gate two_perl = perl_alloc(); 927*0Sstevel@tonic-gate 928*0Sstevel@tonic-gate PERL_SET_CONTEXT(one_perl); 929*0Sstevel@tonic-gate perl_construct(one_perl); 930*0Sstevel@tonic-gate PERL_SET_CONTEXT(two_perl); 931*0Sstevel@tonic-gate perl_construct(two_perl); 932*0Sstevel@tonic-gate 933*0Sstevel@tonic-gate PERL_SET_CONTEXT(one_perl); 934*0Sstevel@tonic-gate perl_parse(one_perl, NULL, 3, one_args, (char **)NULL); 935*0Sstevel@tonic-gate PERL_SET_CONTEXT(two_perl); 936*0Sstevel@tonic-gate perl_parse(two_perl, NULL, 3, two_args, (char **)NULL); 937*0Sstevel@tonic-gate 938*0Sstevel@tonic-gate PERL_SET_CONTEXT(one_perl); 939*0Sstevel@tonic-gate perl_run(one_perl); 940*0Sstevel@tonic-gate PERL_SET_CONTEXT(two_perl); 941*0Sstevel@tonic-gate perl_run(two_perl); 942*0Sstevel@tonic-gate 943*0Sstevel@tonic-gate PERL_SET_CONTEXT(one_perl); 944*0Sstevel@tonic-gate perl_destruct(one_perl); 945*0Sstevel@tonic-gate PERL_SET_CONTEXT(two_perl); 946*0Sstevel@tonic-gate perl_destruct(two_perl); 947*0Sstevel@tonic-gate 948*0Sstevel@tonic-gate PERL_SET_CONTEXT(one_perl); 949*0Sstevel@tonic-gate perl_free(one_perl); 950*0Sstevel@tonic-gate PERL_SET_CONTEXT(two_perl); 951*0Sstevel@tonic-gate perl_free(two_perl); 952*0Sstevel@tonic-gate PERL_SYS_TERM(); 953*0Sstevel@tonic-gate } 954*0Sstevel@tonic-gate 955*0Sstevel@tonic-gateNote the calls to PERL_SET_CONTEXT(). These are necessary to initialize 956*0Sstevel@tonic-gatethe global state that tracks which interpreter is the "current" one on 957*0Sstevel@tonic-gatethe particular process or thread that may be running it. It should 958*0Sstevel@tonic-gatealways be used if you have more than one interpreter and are making 959*0Sstevel@tonic-gateperl API calls on both interpreters in an interleaved fashion. 960*0Sstevel@tonic-gate 961*0Sstevel@tonic-gatePERL_SET_CONTEXT(interp) should also be called whenever C<interp> is 962*0Sstevel@tonic-gateused by a thread that did not create it (using either perl_alloc(), or 963*0Sstevel@tonic-gatethe more esoteric perl_clone()). 964*0Sstevel@tonic-gate 965*0Sstevel@tonic-gateCompile as usual: 966*0Sstevel@tonic-gate 967*0Sstevel@tonic-gate % cc -o multiplicity multiplicity.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 968*0Sstevel@tonic-gate 969*0Sstevel@tonic-gateRun it, Run it: 970*0Sstevel@tonic-gate 971*0Sstevel@tonic-gate % multiplicity 972*0Sstevel@tonic-gate Hi, I'm one_perl 973*0Sstevel@tonic-gate Hi, I'm two_perl 974*0Sstevel@tonic-gate 975*0Sstevel@tonic-gate=head2 Using Perl modules, which themselves use C libraries, from your C program 976*0Sstevel@tonic-gate 977*0Sstevel@tonic-gateIf you've played with the examples above and tried to embed a script 978*0Sstevel@tonic-gatethat I<use()>s a Perl module (such as I<Socket>) which itself uses a C or C++ library, 979*0Sstevel@tonic-gatethis probably happened: 980*0Sstevel@tonic-gate 981*0Sstevel@tonic-gate 982*0Sstevel@tonic-gate Can't load module Socket, dynamic loading not available in this perl. 983*0Sstevel@tonic-gate (You may need to build a new perl executable which either supports 984*0Sstevel@tonic-gate dynamic loading or has the Socket module statically linked into it.) 985*0Sstevel@tonic-gate 986*0Sstevel@tonic-gate 987*0Sstevel@tonic-gateWhat's wrong? 988*0Sstevel@tonic-gate 989*0Sstevel@tonic-gateYour interpreter doesn't know how to communicate with these extensions 990*0Sstevel@tonic-gateon its own. A little glue will help. Up until now you've been 991*0Sstevel@tonic-gatecalling I<perl_parse()>, handing it NULL for the second argument: 992*0Sstevel@tonic-gate 993*0Sstevel@tonic-gate perl_parse(my_perl, NULL, argc, my_argv, NULL); 994*0Sstevel@tonic-gate 995*0Sstevel@tonic-gateThat's where the glue code can be inserted to create the initial contact between 996*0Sstevel@tonic-gatePerl and linked C/C++ routines. Let's take a look some pieces of I<perlmain.c> 997*0Sstevel@tonic-gateto see how Perl does this: 998*0Sstevel@tonic-gate 999*0Sstevel@tonic-gate static void xs_init (pTHX); 1000*0Sstevel@tonic-gate 1001*0Sstevel@tonic-gate EXTERN_C void boot_DynaLoader (pTHX_ CV* cv); 1002*0Sstevel@tonic-gate EXTERN_C void boot_Socket (pTHX_ CV* cv); 1003*0Sstevel@tonic-gate 1004*0Sstevel@tonic-gate 1005*0Sstevel@tonic-gate EXTERN_C void 1006*0Sstevel@tonic-gate xs_init(pTHX) 1007*0Sstevel@tonic-gate { 1008*0Sstevel@tonic-gate char *file = __FILE__; 1009*0Sstevel@tonic-gate /* DynaLoader is a special case */ 1010*0Sstevel@tonic-gate newXS("DynaLoader::boot_DynaLoader", boot_DynaLoader, file); 1011*0Sstevel@tonic-gate newXS("Socket::bootstrap", boot_Socket, file); 1012*0Sstevel@tonic-gate } 1013*0Sstevel@tonic-gate 1014*0Sstevel@tonic-gateSimply put: for each extension linked with your Perl executable 1015*0Sstevel@tonic-gate(determined during its initial configuration on your 1016*0Sstevel@tonic-gatecomputer or when adding a new extension), 1017*0Sstevel@tonic-gatea Perl subroutine is created to incorporate the extension's 1018*0Sstevel@tonic-gateroutines. Normally, that subroutine is named 1019*0Sstevel@tonic-gateI<Module::bootstrap()> and is invoked when you say I<use Module>. In 1020*0Sstevel@tonic-gateturn, this hooks into an XSUB, I<boot_Module>, which creates a Perl 1021*0Sstevel@tonic-gatecounterpart for each of the extension's XSUBs. Don't worry about this 1022*0Sstevel@tonic-gatepart; leave that to the I<xsubpp> and extension authors. If your 1023*0Sstevel@tonic-gateextension is dynamically loaded, DynaLoader creates I<Module::bootstrap()> 1024*0Sstevel@tonic-gatefor you on the fly. In fact, if you have a working DynaLoader then there 1025*0Sstevel@tonic-gateis rarely any need to link in any other extensions statically. 1026*0Sstevel@tonic-gate 1027*0Sstevel@tonic-gate 1028*0Sstevel@tonic-gateOnce you have this code, slap it into the second argument of I<perl_parse()>: 1029*0Sstevel@tonic-gate 1030*0Sstevel@tonic-gate 1031*0Sstevel@tonic-gate perl_parse(my_perl, xs_init, argc, my_argv, NULL); 1032*0Sstevel@tonic-gate 1033*0Sstevel@tonic-gate 1034*0Sstevel@tonic-gateThen compile: 1035*0Sstevel@tonic-gate 1036*0Sstevel@tonic-gate % cc -o interp interp.c `perl -MExtUtils::Embed -e ccopts -e ldopts` 1037*0Sstevel@tonic-gate 1038*0Sstevel@tonic-gate % interp 1039*0Sstevel@tonic-gate use Socket; 1040*0Sstevel@tonic-gate use SomeDynamicallyLoadedModule; 1041*0Sstevel@tonic-gate 1042*0Sstevel@tonic-gate print "Now I can use extensions!\n"' 1043*0Sstevel@tonic-gate 1044*0Sstevel@tonic-gateB<ExtUtils::Embed> can also automate writing the I<xs_init> glue code. 1045*0Sstevel@tonic-gate 1046*0Sstevel@tonic-gate % perl -MExtUtils::Embed -e xsinit -- -o perlxsi.c 1047*0Sstevel@tonic-gate % cc -c perlxsi.c `perl -MExtUtils::Embed -e ccopts` 1048*0Sstevel@tonic-gate % cc -c interp.c `perl -MExtUtils::Embed -e ccopts` 1049*0Sstevel@tonic-gate % cc -o interp perlxsi.o interp.o `perl -MExtUtils::Embed -e ldopts` 1050*0Sstevel@tonic-gate 1051*0Sstevel@tonic-gateConsult L<perlxs>, L<perlguts>, and L<perlapi> for more details. 1052*0Sstevel@tonic-gate 1053*0Sstevel@tonic-gate=head1 Embedding Perl under Win32 1054*0Sstevel@tonic-gate 1055*0Sstevel@tonic-gateIn general, all of the source code shown here should work unmodified under 1056*0Sstevel@tonic-gateWindows. 1057*0Sstevel@tonic-gate 1058*0Sstevel@tonic-gateHowever, there are some caveats about the command-line examples shown. 1059*0Sstevel@tonic-gateFor starters, backticks won't work under the Win32 native command shell. 1060*0Sstevel@tonic-gateThe ExtUtils::Embed kit on CPAN ships with a script called 1061*0Sstevel@tonic-gateB<genmake>, which generates a simple makefile to build a program from 1062*0Sstevel@tonic-gatea single C source file. It can be used like this: 1063*0Sstevel@tonic-gate 1064*0Sstevel@tonic-gate C:\ExtUtils-Embed\eg> perl genmake interp.c 1065*0Sstevel@tonic-gate C:\ExtUtils-Embed\eg> nmake 1066*0Sstevel@tonic-gate C:\ExtUtils-Embed\eg> interp -e "print qq{I'm embedded in Win32!\n}" 1067*0Sstevel@tonic-gate 1068*0Sstevel@tonic-gateYou may wish to use a more robust environment such as the Microsoft 1069*0Sstevel@tonic-gateDeveloper Studio. In this case, run this to generate perlxsi.c: 1070*0Sstevel@tonic-gate 1071*0Sstevel@tonic-gate perl -MExtUtils::Embed -e xsinit 1072*0Sstevel@tonic-gate 1073*0Sstevel@tonic-gateCreate a new project and Insert -> Files into Project: perlxsi.c, 1074*0Sstevel@tonic-gateperl.lib, and your own source files, e.g. interp.c. Typically you'll 1075*0Sstevel@tonic-gatefind perl.lib in B<C:\perl\lib\CORE>, if not, you should see the 1076*0Sstevel@tonic-gateB<CORE> directory relative to C<perl -V:archlib>. The studio will 1077*0Sstevel@tonic-gatealso need this path so it knows where to find Perl include files. 1078*0Sstevel@tonic-gateThis path can be added via the Tools -> Options -> Directories menu. 1079*0Sstevel@tonic-gateFinally, select Build -> Build interp.exe and you're ready to go. 1080*0Sstevel@tonic-gate 1081*0Sstevel@tonic-gate=head1 Hiding Perl_ 1082*0Sstevel@tonic-gate 1083*0Sstevel@tonic-gateIf you completely hide the short forms forms of the Perl public API, 1084*0Sstevel@tonic-gateadd -DPERL_NO_SHORT_NAMES to the compilation flags. This means that 1085*0Sstevel@tonic-gatefor example instead of writing 1086*0Sstevel@tonic-gate 1087*0Sstevel@tonic-gate warn("%d bottles of beer on the wall", bottlecount); 1088*0Sstevel@tonic-gate 1089*0Sstevel@tonic-gateyou will have to write the explicit full form 1090*0Sstevel@tonic-gate 1091*0Sstevel@tonic-gate Perl_warn(aTHX_ "%d bottles of beer on the wall", bottlecount); 1092*0Sstevel@tonic-gate 1093*0Sstevel@tonic-gate(See L<perlguts/Background and PERL_IMPLICIT_CONTEXT for the explanation 1094*0Sstevel@tonic-gateof the C<aTHX_>.> ) Hiding the short forms is very useful for avoiding 1095*0Sstevel@tonic-gateall sorts of nasty (C preprocessor or otherwise) conflicts with other 1096*0Sstevel@tonic-gatesoftware packages (Perl defines about 2400 APIs with these short names, 1097*0Sstevel@tonic-gatetake or leave few hundred, so there certainly is room for conflict.) 1098*0Sstevel@tonic-gate 1099*0Sstevel@tonic-gate=head1 MORAL 1100*0Sstevel@tonic-gate 1101*0Sstevel@tonic-gateYou can sometimes I<write faster code> in C, but 1102*0Sstevel@tonic-gateyou can always I<write code faster> in Perl. Because you can use 1103*0Sstevel@tonic-gateeach from the other, combine them as you wish. 1104*0Sstevel@tonic-gate 1105*0Sstevel@tonic-gate 1106*0Sstevel@tonic-gate=head1 AUTHOR 1107*0Sstevel@tonic-gate 1108*0Sstevel@tonic-gateJon Orwant <F<orwant@media.mit.edu>> and Doug MacEachern 1109*0Sstevel@tonic-gate<F<dougm@covalent.net>>, with small contributions from Tim Bunce, Tom 1110*0Sstevel@tonic-gateChristiansen, Guy Decoux, Hallvard Furuseth, Dov Grobgeld, and Ilya 1111*0Sstevel@tonic-gateZakharevich. 1112*0Sstevel@tonic-gate 1113*0Sstevel@tonic-gateDoug MacEachern has an article on embedding in Volume 1, Issue 4 of 1114*0Sstevel@tonic-gateThe Perl Journal ( http://www.tpj.com/ ). Doug is also the developer of the 1115*0Sstevel@tonic-gatemost widely-used Perl embedding: the mod_perl system 1116*0Sstevel@tonic-gate(perl.apache.org), which embeds Perl in the Apache web server. 1117*0Sstevel@tonic-gateOracle, Binary Evolution, ActiveState, and Ben Sugars's nsapi_perl 1118*0Sstevel@tonic-gatehave used this model for Oracle, Netscape and Internet Information 1119*0Sstevel@tonic-gateServer Perl plugins. 1120*0Sstevel@tonic-gate 1121*0Sstevel@tonic-gateJuly 22, 1998 1122*0Sstevel@tonic-gate 1123*0Sstevel@tonic-gate=head1 COPYRIGHT 1124*0Sstevel@tonic-gate 1125*0Sstevel@tonic-gateCopyright (C) 1995, 1996, 1997, 1998 Doug MacEachern and Jon Orwant. All 1126*0Sstevel@tonic-gateRights Reserved. 1127*0Sstevel@tonic-gate 1128*0Sstevel@tonic-gatePermission is granted to make and distribute verbatim copies of this 1129*0Sstevel@tonic-gatedocumentation provided the copyright notice and this permission notice are 1130*0Sstevel@tonic-gatepreserved on all copies. 1131*0Sstevel@tonic-gate 1132*0Sstevel@tonic-gatePermission is granted to copy and distribute modified versions of this 1133*0Sstevel@tonic-gatedocumentation under the conditions for verbatim copying, provided also 1134*0Sstevel@tonic-gatethat they are marked clearly as modified versions, that the authors' 1135*0Sstevel@tonic-gatenames and title are unchanged (though subtitles and additional 1136*0Sstevel@tonic-gateauthors' names may be added), and that the entire resulting derived 1137*0Sstevel@tonic-gatework is distributed under the terms of a permission notice identical 1138*0Sstevel@tonic-gateto this one. 1139*0Sstevel@tonic-gate 1140*0Sstevel@tonic-gatePermission is granted to copy and distribute translations of this 1141*0Sstevel@tonic-gatedocumentation into another language, under the above conditions for 1142*0Sstevel@tonic-gatemodified versions. 1143