📄 perlxstut.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 "PERLXSTUT 1".TH PERLXSTUT 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"perlXStut \- Tutorial for writing XSUBs.SH "DESCRIPTION".IX Header "DESCRIPTION"This tutorial will educate the reader on the steps involved in creatinga Perl extension. The reader is assumed to have access to perlguts,perlapi and perlxs..PPThis tutorial starts with very simple examples and becomes more complex,with each new example adding new features. Certain concepts may not becompletely explained until later in the tutorial in order to slowly easethe reader into building extensions..PPThis tutorial was written from a Unix point of view. Where I know themto be otherwise different for other platforms (e.g. Win32), I will listthem. If you find something that was missed, please let me know..SH "SPECIAL NOTES".IX Header "SPECIAL NOTES".Sh "make".IX Subsection "make"This tutorial assumes that the make program that Perl is configured touse is called \f(CW\*(C`make\*(C'\fR. Instead of running \*(L"make\*(R" in the examples thatfollow, you may have to substitute whatever make program Perl has beenconfigured to use. Running \fBperl \-V:make\fR should tell you what it is..Sh "Version caveat".IX Subsection "Version caveat"When writing a Perl extension for general consumption, one should expect thatthe extension will be used with versions of Perl different from theversion available on your machine. Since you are reading this document,the version of Perl on your machine is probably 5.005 or later, but the usersof your extension may have more ancient versions..PPTo understand what kinds of incompatibilities one may expect, and in the rarecase that the version of Perl on your machine is older than this document,see the section on \*(L"Troubleshooting these Examples\*(R" for more information..PPIf your extension uses some features of Perl which are not available on olderreleases of Perl, your users would appreciate an early meaningful warning.You would probably put this information into the \fI\s-1README\s0\fR file, but nowadaysinstallation of extensions may be performed automatically, guided by \fI\s-1CPAN\s0.pm\fRmodule or other tools..PPIn MakeMaker-based installations, \fIMakefile.PL\fR provides the earliestopportunity to perform version checks. One can put something like thisin \fIMakefile.PL\fR for this purpose:.PP.Vb 7\& eval { require 5.007 }\& or die <<EOD;\& ############\& ### This module uses frobnication framework which is not available before\& ### version 5.007 of Perl. Upgrade your Perl before installing Kara::Mba.\& ############\& EOD.Ve.Sh "Dynamic Loading versus Static Loading".IX Subsection "Dynamic Loading versus Static Loading"It is commonly thought that if a system does not have the capability todynamically load a library, you cannot build XSUBs. This is incorrect.You \fIcan\fR build them, but you must link the XSUBs subroutines with therest of Perl, creating a new executable. This situation is similar toPerl 4..PPThis tutorial can still be used on such a system. The \s-1XSUB\s0 build mechanismwill check the system and build a dynamically-loadable library if possible,or else a static library and then, optionally, a new statically-linkedexecutable with that static library linked in..PPShould you wish to build a statically-linked executable on a system whichcan dynamically load libraries, you may, in all the following examples,where the command "\f(CW\*(C`make\*(C'\fR\*(L" with no arguments is executed, run the command\&\*(R"\f(CW\*(C`make perl\*(C'\fR" instead..PPIf you have generated such a statically-linked executable by choice, theninstead of saying "\f(CW\*(C`make test\*(C'\fR\*(L", you should say \*(R"\f(CW\*(C`make test_static\*(C'\fR\*(L".On systems that cannot build dynamically-loadable libraries at all, simplysaying \*(R"\f(CW\*(C`make test\*(C'\fR" is sufficient..SH "TUTORIAL".IX Header "TUTORIAL"Now let's go on with the show!.Sh "\s-1EXAMPLE\s0 1".IX Subsection "EXAMPLE 1"Our first extension will be very simple. When we call the routine in theextension, it will print out a well-known message and return..PPRun "\f(CW\*(C`h2xs \-A \-n Mytest\*(C'\fR". This creates a directory named Mytest,possibly under ext/ if that directory exists in the current workingdirectory. Several files will be created in the Mytest dir, including\&\s-1MANIFEST\s0, Makefile.PL, Mytest.pm, Mytest.xs, Mytest.t, and Changes..PPThe \s-1MANIFEST\s0 file contains the names of all the files just created in theMytest directory..PPThe file Makefile.PL should look something like this:.PP.Vb 10\& use ExtUtils::MakeMaker;\& # See lib/ExtUtils/MakeMaker.pm for details of how to influence\& # the contents of the Makefile that is written.\& WriteMakefile(\& NAME => \*(AqMytest\*(Aq,\& VERSION_FROM => \*(AqMytest.pm\*(Aq, # finds $VERSION\& LIBS => [\*(Aq\*(Aq], # e.g., \*(Aq\-lm\*(Aq\& DEFINE => \*(Aq\*(Aq, # e.g., \*(Aq\-DHAVE_SOMETHING\*(Aq\& INC => \*(Aq\*(Aq, # e.g., \*(Aq\-I/usr/include/other\*(Aq\& );.Ve.PPThe file Mytest.pm should start with something like this:.PP.Vb 1\& package Mytest;\&\& use 5.008008;\& use strict;\& use warnings;\&\& require Exporter;\&\& our @ISA = qw(Exporter);\& our %EXPORT_TAGS = ( \*(Aqall\*(Aq => [ qw(\&\& ) ] );\&\& our @EXPORT_OK = ( @{ $EXPORT_TAGS{\*(Aqall\*(Aq} } );\&\& our @EXPORT = qw(\&\& );\&\& our $VERSION = \*(Aq0.01\*(Aq;\&\& require XSLoader;\& XSLoader::load(\*(AqMytest\*(Aq, $VERSION);\&\& # Preloaded methods go here.\&\& 1;\& _\|_END_\|_\& # Below is the stub of documentation for your module. You better edit it!.Ve.PPThe rest of the .pm file contains sample code for providing documentation forthe extension..PPFinally, the Mytest.xs file should look something like this:.PP.Vb 3\& #include "EXTERN.h"\& #include "perl.h"\& #include "XSUB.h"\&\& #include "ppport.h"\&\& MODULE = Mytest PACKAGE = Mytest.Ve.PPLet's edit the .xs file by adding this to the end of the file:.PP.Vb 4\& void\& hello()\& CODE:\& printf("Hello, world!\en");.Ve.PPIt is okay for the lines starting at the \*(L"\s-1CODE:\s0\*(R" line to not be indented.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -