📄 perlembed.1
字号:
.\" Automatically generated by Pod::Man 2.16 (Pod::Simple 3.05).\".\" Standard preamble:.\" ========================================================================.de Sh \" Subsection heading.br.if t .Sp.ne 5.PP\fB\\$1\fR.PP...de Sp \" Vertical space (when we can't use .PP).if t .sp .5v.if n .sp...de Vb \" Begin verbatim text.ft CW.nf.ne \\$1...de Ve \" End verbatim text.ft R.fi...\" Set up some character translations and predefined strings. \*(-- will.\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left.\" double quote, and \*(R" will give a right double quote. \*(C+ will.\" give a nicer C++. Capital omega is used to do unbreakable dashes and.\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff,.\" nothing in troff, for use with C<>..tr \(*W-.ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p'.ie n \{\. ds -- \(*W-. ds PI pi. if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch. if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch. ds L" "". ds R" "". ds C` "". ds C' ""'br\}.el\{\. ds -- \|\(em\|. ds PI \(*p. ds L" ``. ds R" '''br\}.\".\" Escape single quotes in literal strings from groff's Unicode transform..ie \n(.g .ds Aq \(aq.el .ds Aq '.\".\" If the F register is turned on, we'll generate index entries on stderr for.\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index.\" entries marked with X<> in POD. Of course, you'll have to process the.\" output yourself in some meaningful fashion..ie \nF \{\. de IX. tm Index:\\$1\t\\n%\t"\\$2"... nr % 0. rr F.\}.el \{\. de IX...\}.\".\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2)..\" Fear. Run. Save yourself. No user-serviceable parts.. \" fudge factors for nroff and troff.if n \{\. ds #H 0. ds #V .8m. ds #F .3m. ds #[ \f1. ds #] \fP.\}.if t \{\. ds #H ((1u-(\\\\n(.fu%2u))*.13m). ds #V .6m. ds #F 0. ds #[ \&. ds #] \&.\}. \" simple accents for nroff and troff.if n \{\. ds ' \&. ds ` \&. ds ^ \&. ds , \&. ds ~ ~. ds /.\}.if t \{\. ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u". ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u'. ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u'. ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u'. ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u'. ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u'.\}. \" troff and (daisy-wheel) nroff accents.ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V'.ds 8 \h'\*(#H'\(*b\h'-\*(#H'.ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#].ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H'.ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u'.ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#].ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#].ds ae a\h'-(\w'a'u*4/10)'e.ds Ae A\h'-(\w'A'u*4/10)'E. \" corrections for vroff.if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u'.if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u'. \" for low resolution devices (crt and lpr).if \n(.H>23 .if \n(.V>19 \\{\. ds : e. ds 8 ss. ds o a. ds d- d\h'-1'\(ga. ds D- D\h'-1'\(hy. ds th \o'bp'. ds Th \o'LP'. ds ae ae. ds Ae AE.\}.rm #[ #] #H #V #F C.\" ========================================================================.\".IX Title "PERLEMBED 1".TH PERLEMBED 1 "2007-12-18" "perl v5.10.0" "Perl Programmers Reference Guide".\" For nroff, turn off justification. Always turn off hyphenation; it makes.\" way too many mistakes in technical documents..if n .ad l.nh.SH "NAME"perlembed \- how to embed perl in your C program.SH "DESCRIPTION".IX Header "DESCRIPTION".Sh "\s-1PREAMBLE\s0".IX Subsection "PREAMBLE"Do you want to:.IP "\fBUse C from Perl?\fR" 5.IX Item "Use C from Perl?"Read perlxstut, perlxs, h2xs, perlguts, and perlapi..IP "\fBUse a Unix program from Perl?\fR" 5.IX Item "Use a Unix program from Perl?"Read about back-quotes and about \f(CW\*(C`system\*(C'\fR and \f(CW\*(C`exec\*(C'\fR in perlfunc..IP "\fBUse Perl from Perl?\fR" 5.IX Item "Use Perl from Perl?"Read about \*(L"do\*(R" in perlfunc and \*(L"eval\*(R" in perlfunc and \*(L"require\*(R" in perlfunc and \*(L"use\*(R" in perlfunc..IP "\fBUse C from C?\fR" 5.IX Item "Use C from C?"Rethink your design..IP "\fBUse Perl from C?\fR" 5.IX Item "Use Perl from C?"Read on....Sh "\s-1ROADMAP\s0".IX Subsection "ROADMAP".IP "\(bu" 5Compiling your C program.IP "\(bu" 5Adding a Perl interpreter to your C program.IP "\(bu" 5Calling a Perl subroutine from your C program.IP "\(bu" 5Evaluating a Perl statement from your C program.IP "\(bu" 5Performing Perl pattern matches and substitutions from your C program.IP "\(bu" 5Fiddling with the Perl stack from your C program.IP "\(bu" 5Maintaining a persistent interpreter.IP "\(bu" 5Maintaining multiple interpreter instances.IP "\(bu" 5Using Perl modules, which themselves use C libraries, from your C program.IP "\(bu" 5Embedding Perl under Win32.Sh "Compiling your C program".IX Subsection "Compiling your C program"If you have trouble compiling the scripts in this documentation,you're not alone. The cardinal rule: \s-1COMPILE\s0 \s-1THE\s0 \s-1PROGRAMS\s0 \s-1IN\s0 \s-1EXACTLY\s0\&\s-1THE\s0 \s-1SAME\s0 \s-1WAY\s0 \s-1THAT\s0 \s-1YOUR\s0 \s-1PERL\s0 \s-1WAS\s0 \s-1COMPILED\s0. (Sorry for yelling.).PPAlso, every C program that uses Perl must link in the \fIperl library\fR.What's that, you ask? Perl is itself written in C; the perl libraryis the collection of compiled C programs that were used to create yourperl executable (\fI/usr/bin/perl\fR or equivalent). (Corollary: youcan't use Perl from your C program unless Perl has been compiled onyour machine, or installed properly\*(--that's why you shouldn't blithelycopy Perl executables from machine to machine without also copying the\&\fIlib\fR directory.).PPWhen you use Perl from C, your C program will\*(--usually\-\-allocate,\&\*(L"run\*(R", and deallocate a \fIPerlInterpreter\fR object, which is defined bythe perl library..PPIf your copy of Perl is recent enough to contain this documentation(version 5.002 or later), then the perl library (and \fI\s-1EXTERN\s0.h\fR and\&\fIperl.h\fR, which you'll also need) will reside in a directorythat looks like this:.PP.Vb 1\& /usr/local/lib/perl5/your_architecture_here/CORE.Ve.PPor perhaps just.PP.Vb 1\& /usr/local/lib/perl5/CORE.Ve.PPor maybe something like.PP.Vb 1\& /usr/opt/perl5/CORE.Ve.PPExecute this statement for a hint about where to find \s-1CORE:\s0.PP.Vb 1\& perl \-MConfig \-e \*(Aqprint $Config{archlib}\*(Aq.Ve.PPHere's how you'd compile the example in the next section,\&\*(L"Adding a Perl interpreter to your C program\*(R", on my Linux box:.PP.Vb 4\& % gcc \-O2 \-Dbool=char \-DHAS_BOOL \-I/usr/local/include\& \-I/usr/local/lib/perl5/i586\-linux/5.003/CORE\& \-L/usr/local/lib/perl5/i586\-linux/5.003/CORE\& \-o interp interp.c \-lperl \-lm.Ve.PP(That's all one line.) On my \s-1DEC\s0 Alpha running old 5.003_05, the incantation is a bit different:.PP.Vb 4\& % cc \-O2 \-Olimit 2900 \-DSTANDARD_C \-I/usr/local/include\& \-I/usr/local/lib/perl5/alpha\-dec_osf/5.00305/CORE\& \-L/usr/local/lib/perl5/alpha\-dec_osf/5.00305/CORE \-L/usr/local/lib\& \-D_\|_LANGUAGE_C_\|_ \-D_NO_PROTO \-o interp interp.c \-lperl \-lm.Ve.PPHow can you figure out what to add? Assuming your Perl is post\-5.001,execute a \f(CW\*(C`perl \-V\*(C'\fR command and pay special attention to the \*(L"cc\*(R" and\&\*(L"ccflags\*(R" information..PPYou'll have to choose the appropriate compiler (\fIcc\fR, \fIgcc\fR, et al.) foryour machine: \f(CW\*(C`perl \-MConfig \-e \*(Aqprint $Config{cc}\*(Aq\*(C'\fR will tell you whatto use..PPYou'll also have to choose the appropriate library directory(\fI/usr/local/lib/...\fR) for your machine. If your compiler complainsthat certain functions are undefined, or that it can't locate\&\fI\-lperl\fR, then you need to change the path following the \f(CW\*(C`\-L\*(C'\fR. If itcomplains that it can't find \fI\s-1EXTERN\s0.h\fR and \fIperl.h\fR, you need tochange the path following the \f(CW\*(C`\-I\*(C'\fR..PPYou may have to add extra libraries as well. Which ones?Perhaps those printed by.PP.Vb 1\& perl \-MConfig \-e \*(Aqprint $Config{libs}\*(Aq.Ve.PPProvided your perl binary was properly configured and installed the\&\fBExtUtils::Embed\fR module will determine all of this information foryou:.PP.Vb 1\& % cc \-o interp interp.c \`perl \-MExtUtils::Embed \-e ccopts \-e ldopts\`.Ve.PPIf the \fBExtUtils::Embed\fR module isn't part of your Perl distribution,you can retrieve it fromhttp://www.perl.com/perl/CPAN/modules/by\-module/ExtUtils/(If this documentation came from your Perl distribution, then you'rerunning 5.004 or better and you already have it.).PPThe \fBExtUtils::Embed\fR kit on \s-1CPAN\s0 also contains all source code forthe examples in this document, tests, additional examples and otherinformation you may find useful..Sh "Adding a Perl interpreter to your C program".IX Subsection "Adding a Perl interpreter to your C program"In a sense, perl (the C program) is a good example of embedding Perl(the language), so I'll demonstrate embedding with \fIminiperlmain.c\fR,included in the source distribution. Here's a bastardized, non-portableversion of \fIminiperlmain.c\fR containing the essentials of embedding:.PP.Vb 2\& #include <EXTERN.h> /* from the Perl distribution */\& #include <perl.h> /* from the Perl distribution */\&\& static PerlInterpreter *my_perl; /*** The Perl interpreter ***/\&\& int main(int argc, char **argv, char **env)\& {\& PERL_SYS_INIT3(&argc,&argv,&env);\& my_perl = perl_alloc();\& perl_construct(my_perl);\& PL_exit_flags |= PERL_EXIT_DESTRUCT_END;\& perl_parse(my_perl, NULL, argc, argv, (char **)NULL);\& perl_run(my_perl);\& perl_destruct(my_perl);\& perl_free(my_perl);\& PERL_SYS_TERM();\& }.Ve.PPNotice that we don't use the \f(CW\*(C`env\*(C'\fR pointer. Normally handed to\&\f(CW\*(C`perl_parse\*(C'\fR as its final argument, \f(CW\*(C`env\*(C'\fR here is replaced by\&\f(CW\*(C`NULL\*(C'\fR, which means that the current environment will be used. The macros\&\s-1\fIPERL_SYS_INIT3\s0()\fR and \s-1\fIPERL_SYS_TERM\s0()\fR provide system-specific tune up of the C runtime environment necessary to run Perl interpreters; since\&\s-1\fIPERL_SYS_INIT3\s0()\fR may change \f(CW\*(C`env\*(C'\fR, it may be more appropriate to provide\&\f(CW\*(C`env\*(C'\fR as an argument to \fIperl_parse()\fR..PPNow compile this program (I'll call it \fIinterp.c\fR) into an executable:.PP.Vb 1\& % cc \-o interp interp.c \`perl \-MExtUtils::Embed \-e ccopts \-e ldopts\`.Ve.PPAfter a successful compilation, you'll be able to use \fIinterp\fR justlike perl itself:
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -