📄 perlxs.1
字号:
\& char * s\& char &c.Ve.PPBoth these \s-1XS\s0 declarations correspond to the \f(CW\*(C`char*\*(C'\fR C type, but they havedifferent semantics, see \*(L"The & Unary Operator\*(R"..PPIt is convenient to think that the indirection operator\&\f(CW\*(C`*\*(C'\fR should be considered as a part of the type and the address operator \f(CW\*(C`&\*(C'\fRshould be considered part of the variable. See \*(L"The Typemap\*(R"for more info about handling qualifiers and unary operators in C types..PPThe function name and the return type must be placed onseparate lines and should be flush left-adjusted..PP.Vb 1\& INCORRECT CORRECT\&\& double sin(x) double\& double x sin(x)\& double x.Ve.PPThe rest of the function description may be indented or left-adjusted. Thefollowing example shows a function with its body left-adjusted. Mostexamples in this document will indent the body for better readability..PP.Vb 1\& CORRECT\&\& double\& sin(x)\& double x.Ve.PPMore complicated XSUBs may contain many other sections. Each section ofan \s-1XSUB\s0 starts with the corresponding keyword, such as \s-1INIT:\s0 or \s-1CLEANUP:\s0.However, the first two lines of an \s-1XSUB\s0 always contain the same data:descriptions of the return type and the names of the function and itsparameters. Whatever immediately follows these is considered to bean \s-1INPUT:\s0 section unless explicitly marked with another keyword.(See \*(L"The \s-1INPUT:\s0 Keyword\*(R".).PPAn \s-1XSUB\s0 section continues until another section-start keyword is found..Sh "The Argument Stack".IX Subsection "The Argument Stack"The Perl argument stack is used to store the values which aresent as parameters to the \s-1XSUB\s0 and to store the \s-1XSUB\s0'sreturn value(s). In reality all Perl functions (including non-XSUBones) keep their values on this stack all the same time, each limitedto its own range of positions on the stack. In this document thefirst position on that stack which belongs to the activefunction will be referred to as position 0 for that function..PPXSUBs refer to their stack arguments with the macro \fB\s-1ST\s0(x)\fR, where \fIx\fRrefers to a position in this \s-1XSUB\s0's part of the stack. Position 0 for thatfunction would be known to the \s-1XSUB\s0 as \s-1\fIST\s0\fR\|(0). The \s-1XSUB\s0's incomingparameters and outgoing return values always begin at \s-1\fIST\s0\fR\|(0). For manysimple cases the \fBxsubpp\fR compiler will generate the code necessary tohandle the argument stack by embedding code fragments found in thetypemaps. In more complex cases the programmer must supply the code..Sh "The \s-1RETVAL\s0 Variable".IX Subsection "The RETVAL Variable"The \s-1RETVAL\s0 variable is a special C variable that is declared automaticallyfor you. The C type of \s-1RETVAL\s0 matches the return type of the C libraryfunction. The \fBxsubpp\fR compiler will declare this variable in each \s-1XSUB\s0with non\-\f(CW\*(C`void\*(C'\fR return type. By default the generated C functionwill use \s-1RETVAL\s0 to hold the return value of the C library function beingcalled. In simple cases the value of \s-1RETVAL\s0 will be placed in \s-1\fIST\s0\fR\|(0) ofthe argument stack where it can be received by Perl as the return valueof the \s-1XSUB\s0..PPIf the \s-1XSUB\s0 has a return type of \f(CW\*(C`void\*(C'\fR then the compiler willnot declare a \s-1RETVAL\s0 variable for that function. When usinga \s-1PPCODE:\s0 section no manipulation of the \s-1RETVAL\s0 variable is required, thesection may use direct stack manipulation to place output values on the stack..PPIf \s-1PPCODE:\s0 directive is not used, \f(CW\*(C`void\*(C'\fR return value should be usedonly for subroutines which do not return a value, \fIeven if\fR \s-1CODE:\s0directive is used which sets \s-1\fIST\s0\fR\|(0) explicitly..PPOlder versions of this document recommended to use \f(CW\*(C`void\*(C'\fR returnvalue in such cases. It was discovered that this could lead tosegfaults in cases when \s-1XSUB\s0 was \fItruly\fR \f(CW\*(C`void\*(C'\fR. This practice isnow deprecated, and may be not supported at some future version. Usethe return value \f(CW\*(C`SV *\*(C'\fR in such cases. (Currently \f(CW\*(C`xsubpp\*(C'\fR containssome heuristic code which tries to disambiguate between \*(L"truly-void\*(R"and \*(L"old-practice-declared-as-void\*(R" functions. Hence your code is atmercy of this heuristics unless you use \f(CW\*(C`SV *\*(C'\fR as return value.).Sh "Returning SVs, AVs and HVs through \s-1RETVAL\s0".IX Subsection "Returning SVs, AVs and HVs through RETVAL"When you're using \s-1RETVAL\s0 to return an \f(CW\*(C`SV *\*(C'\fR, there's some magicgoing on behind the scenes that should be mentioned. When you'remanipulating the argument stack using the \s-1ST\s0(x) macro, for example,you usually have to pay special attention to reference counts. (Formore about reference counts, see perlguts.) To make your lifeeasier, the typemap file automatically makes \f(CW\*(C`RETVAL\*(C'\fR mortal whenyou're returning an \f(CW\*(C`SV *\*(C'\fR. Thus, the following two XSUBs are moreor less equivalent:.PP.Vb 6\& void\& alpha()\& PPCODE:\& ST(0) = newSVpv("Hello World",0);\& sv_2mortal(ST(0));\& XSRETURN(1);\& \& SV *\& beta()\& CODE:\& RETVAL = newSVpv("Hello World",0);\& OUTPUT:\& RETVAL.Ve.PPThis is quite useful as it usually improves readability. Whilethis works fine for an \f(CW\*(C`SV *\*(C'\fR, it's unfortunately not as easyto have \f(CW\*(C`AV *\*(C'\fR or \f(CW\*(C`HV *\*(C'\fR as a return value. You \fIshould\fR beable to write:.PP.Vb 7\& AV *\& array()\& CODE:\& RETVAL = newAV();\& /* do something with RETVAL */\& OUTPUT:\& RETVAL.Ve.PPBut due to an unfixable bug (fixing it would break lots of existing\&\s-1CPAN\s0 modules) in the typemap file, the reference count of the \f(CW\*(C`AV *\*(C'\fRis not properly decremented. Thus, the above \s-1XSUB\s0 would leak memorywhenever it is being called. The same problem exists for \f(CW\*(C`HV *\*(C'\fR..PPWhen you're returning an \f(CW\*(C`AV *\*(C'\fR or a \f(CW\*(C`HV *\*(C'\fR, you have make suretheir reference count is decremented by making the \s-1AV\s0 or \s-1HV\s0 mortal:.PP.Vb 8\& AV *\& array()\& CODE:\& RETVAL = newAV();\& sv_2mortal((SV*)RETVAL);\& /* do something with RETVAL */\& OUTPUT:\& RETVAL.Ve.PPAnd also remember that you don't have to do this for an \f(CW\*(C`SV *\*(C'\fR..Sh "The \s-1MODULE\s0 Keyword".IX Subsection "The MODULE Keyword"The \s-1MODULE\s0 keyword is used to start the \s-1XS\s0 code and to specify the packageof the functions which are being defined. All text preceding the first\&\s-1MODULE\s0 keyword is considered C code and is passed through to the output with\&\s-1POD\s0 stripped, but otherwise untouched. Every \s-1XS\s0 module will have abootstrap function which is used to hook the XSUBs into Perl. The packagename of this bootstrap function will match the value of the last \s-1MODULE\s0statement in the \s-1XS\s0 source files. The value of \s-1MODULE\s0 should always remainconstant within the same \s-1XS\s0 file, though this is not required..PPThe following example will start the \s-1XS\s0 code and will placeall functions in a package named \s-1RPC\s0..PP.Vb 1\& MODULE = RPC.Ve.Sh "The \s-1PACKAGE\s0 Keyword".IX Subsection "The PACKAGE Keyword"When functions within an \s-1XS\s0 source file must be separated into packagesthe \s-1PACKAGE\s0 keyword should be used. This keyword is used with the \s-1MODULE\s0keyword and must follow immediately after it when used..PP.Vb 1\& MODULE = RPC PACKAGE = RPC\&\& [ XS code in package RPC ]\&\& MODULE = RPC PACKAGE = RPCB\&\& [ XS code in package RPCB ]\&\& MODULE = RPC PACKAGE = RPC\&\& [ XS code in package RPC ].Ve.PPThe same package name can be used more than once, allowing fornon-contiguous code. This is useful if you have a stronger orderingprinciple than package names..PPAlthough this keyword is optional and in some cases provides redundantinformation it should always be used. This keyword will ensure that theXSUBs appear in the desired package..Sh "The \s-1PREFIX\s0 Keyword".IX Subsection "The PREFIX Keyword"The \s-1PREFIX\s0 keyword designates prefixes which should beremoved from the Perl function names. If the C function is\&\f(CW\*(C`rpcb_gettime()\*(C'\fR and the \s-1PREFIX\s0 value is \f(CW\*(C`rpcb_\*(C'\fR then Perl willsee this function as \f(CW\*(C`gettime()\*(C'\fR..PPThis keyword should follow the \s-1PACKAGE\s0 keyword when used.If \s-1PACKAGE\s0 is not used then \s-1PREFIX\s0 should follow the \s-1MODULE\s0keyword..PP.Vb 1\& MODULE = RPC PREFIX = rpc_\&\& MODULE = RPC PACKAGE = RPCB PREFIX = rpcb_.Ve.Sh "The \s-1OUTPUT:\s0 Keyword".IX Subsection "The OUTPUT: Keyword"The \s-1OUTPUT:\s0 keyword indicates that certain function parameters should beupdated (new values made visible to Perl) when the \s-1XSUB\s0 terminates or thatcertain values should be returned to the calling Perl function. Forsimple functions which have no \s-1CODE:\s0 or \s-1PPCODE:\s0 section,such as the \fIsin()\fR function above, the \s-1RETVAL\s0 variable isautomatically designated as an output value. For more complex functionsthe \fBxsubpp\fR compiler will need help to determine which variables are outputvariables..PPThis keyword will normally be used to complement the \s-1CODE:\s0 keyword.The \s-1RETVAL\s0 variable is not recognized as an output variable when the\&\s-1CODE:\s0 keyword is present. The \s-1OUTPUT:\s0 keyword is used in thissituation to tell the compiler that \s-1RETVAL\s0 really is an outputvariable..PPThe \s-1OUTPUT:\s0 keyword can also be used to indicate that function parametersare output variables. This may be necessary when a parameter has beenmodified within the function and the programmer would like the update tobe seen by Perl..PP.Vb 6\& bool_t\& rpcb_gettime(host,timep)\& char *host\& time_t &timep\& OUTPUT:\& timep.Ve.PPThe \s-1OUTPUT:\s0 keyword will also allow an output parameter tobe mapped to a matching piece of code rather than to atypemap..PP.Vb 6\& bool_t\& rpcb_gettime(host,timep)\& char *host\& time_t &timep\& OUTPUT:\& timep sv_setnv(ST(1), (double)timep);.Ve.PP\&\fBxsubpp\fR emits an automatic \f(CW\*(C`SvSETMAGIC()\*(C'\fR for all parameters in the\&\s-1OUTPUT\s0 section of the \s-1XSUB\s0, except \s-1RETVAL\s0. This is the usually desiredbehavior, as it takes care of properly invoking 'set' magic on outputparameters (needed for hash or array element parameters that must becreated if they didn't exist). If for some reason, this behavior isnot desired, the \s-1OUTPUT\s0 section may contain a \f(CW\*(C`SETMAGIC: DISABLE\*(C'\fR lineto disable it for the remainder of the parameters in the \s-1OUTPUT\s0 section.Likewise, \f(CW\*(C`SETMAGIC: ENABLE\*(C'\fR can be used to reenable it for theremainder of the \s-1OUTPUT\s0 section. See perlguts for more detailsabout 'set' magic..Sh "The \s-1NO_OUTPUT\s0 Keyword".IX Subsection "The NO_OUTPUT Keyword"The \s-1NO_OUTPUT\s0 can be placed as the first token of the \s-1XSUB\s0. This keywordindicates that while the C subroutine we provide an interface to hasa non\-\f(CW\*(C`void\*(C'\fR return type, the return value of this C subroutine should notbe returned from the generated Perl subroutine..PPWith this keyword present \*(L"The \s-1RETVAL\s0 Variable\*(R" is created, and in thegenerated call to the subroutine this variable is assigned to, but the valueof this variable is not going to be used in the auto-generated code..PPThis keyword makes sense only if \f(CW\*(C`RETVAL\*(C'\fR is going to be accessed by theuser-supplied code. It is especially useful to make a function interfacemore Perl-like, especially when the C return value is just an error conditionindicator. For example,.PP.Vb 5\& NO_OUTPUT int\& delete_file(char *name)\& POSTCALL:\& if (RETVAL != 0)\& croak("Error %d while deleting file \*(Aq%s\*(Aq", RETVAL, name);.Ve.PPHere the generated \s-1XS\s0 function returns nothing on success, and will \fIdie()\fRwith a meaningful error message on error..Sh "The \s-1CODE:\s0 Keyword".IX Subsection "The CODE: Keyword"This keyword is used in more complicated XSUBs which requirespecial handling for the C function. The \s-1RETVAL\s0 variable isstill declared, but it will not be returned unless it is specifiedin the \s-1OUTPUT:\s0 section..PPThe following \s-1XSUB\s0 is for a C function which requires special handling ofits parameters. The Perl usage is given first..PP.Vb 1\& $status = rpcb_gettime( "localhost", $timep );.Ve.PPThe \s-1XSUB\s0 follows..PP.Vb 9\& bool_t\& rpcb_gettime(host,timep)\& char *host\& time_t timep\& CODE:\& RETVAL = rpcb_gettime( host, &timep );\& OUTPUT:\& timep\& RETVAL.Ve.Sh "The \s-1INIT:\s0 Keyword".IX Subsection "The INIT: Keyword"The \s-1INIT:\s0 keyword allows initialization to be inserted into the \s-1XSUB\s0 beforethe compiler generates the call to the C function. Unlike the \s-1CODE:\s0 keywordabove, this keyword does not affect the way the compiler handles \s-1RETVAL\s0..PP.Vb 8\& bool_t\& rpcb_gettime(host,timep)\& char *host\& time_t &timep\& INIT:\& printf("# Host is %s\en", host );\& OUTPUT:\& timep.Ve.PPAnother use for the \s-1INIT:\s0 section is to check for preconditions beforemaking a call to the C function:.PP.Vb 9\& long long\& lldiv(a,b)\& long long a\& long long b\& INIT:\& if (a == 0 && b == 0)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -