perlguts.pod
来自「MSYS在windows下模拟了一个类unix的终端」· POD 代码 · 共 1,666 行 · 第 1/5 页
POD
1,666 行
=head1 NAMEperlguts - Introduction to the Perl API=head1 DESCRIPTIONThis document attempts to describe how to use the Perl API, as well ascontaining some info on the basic workings of the Perl core. It is farfrom complete and probably contains many errors. Please refer anyquestions or comments to the author below.=head1 Variables=head2 DatatypesPerl has three typedefs that handle Perl's three main data types: SV Scalar Value AV Array Value HV Hash ValueEach typedef has specific routines that manipulate the various data types.=head2 What is an "IV"?Perl uses a special typedef IV which is a simple signed integer type that isguaranteed to be large enough to hold a pointer (as well as an integer).Additionally, there is the UV, which is simply an unsigned IV.Perl also uses two special typedefs, I32 and I16, which will always be atleast 32-bits and 16-bits long, respectively. (Again, there are U32 and U16,as well.)=head2 Working with SVsAn SV can be created and loaded with one command. There are four types ofvalues that can be loaded: an integer value (IV), a double (NV),a string (PV), and another scalar (SV).The six routines are: SV* newSViv(IV); SV* newSVnv(double); SV* newSVpv(const char*, int); SV* newSVpvn(const char*, int); SV* newSVpvf(const char*, ...); SV* newSVsv(SV*);To change the value of an *already-existing* SV, there are seven routines: void sv_setiv(SV*, IV); void sv_setuv(SV*, UV); void sv_setnv(SV*, double); void sv_setpv(SV*, const char*); void sv_setpvn(SV*, const char*, int) void sv_setpvf(SV*, const char*, ...); void sv_setpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool); void sv_setsv(SV*, SV*);Notice that you can choose to specify the length of the string to beassigned by using C<sv_setpvn>, C<newSVpvn>, or C<newSVpv>, or you mayallow Perl to calculate the length by using C<sv_setpv> or by specifying0 as the second argument to C<newSVpv>. Be warned, though, that Perl willdetermine the string's length by using C<strlen>, which depends on thestring terminating with a NUL character.The arguments of C<sv_setpvf> are processed like C<sprintf>, and theformatted output becomes the value.C<sv_setpvfn> is an analogue of C<vsprintf>, but it allows you to specifyeither a pointer to a variable argument list or the address and length ofan array of SVs. The last argument points to a boolean; on return, if thatboolean is true, then locale-specific information has been used to formatthe string, and the string's contents are therefore untrustworthy (seeL<perlsec>). This pointer may be NULL if that information is notimportant. Note that this function requires you to specify the length ofthe format.STRLEN is an integer type (Size_t, usually defined as size_t inconfig.h) guaranteed to be large enough to represent the size of any string that perl can handle.The C<sv_set*()> functions are not generic enough to operate on valuesthat have "magic". See L<Magic Virtual Tables> later in this document.All SVs that contain strings should be terminated with a NUL character.If it is not NUL-terminated there is a risk ofcore dumps and corruptions from code which passes the string to Cfunctions or system calls which expect a NUL-terminated string.Perl's own functions typically add a trailing NUL for this reason.Nevertheless, you should be very careful when you pass a string storedin an SV to a C function or system call.To access the actual value that an SV points to, you can use the macros: SvIV(SV*) SvUV(SV*) SvNV(SV*) SvPV(SV*, STRLEN len) SvPV_nolen(SV*)which will automatically coerce the actual scalar type into an IV, UV, double,or string.In the C<SvPV> macro, the length of the string returned is placed into thevariable C<len> (this is a macro, so you do I<not> use C<&len>). If you donot care what the length of the data is, use the C<SvPV_nolen> macro.Historically the C<SvPV> macro with the global variable C<PL_na> has beenused in this case. But that can be quite inefficient because C<PL_na> mustbe accessed in thread-local storage in threaded Perl. In any case, rememberthat Perl allows arbitrary strings of data that may both contain NULs andmight not be terminated by a NUL.Also remember that C doesn't allow you to safely say C<foo(SvPV(s, len),len);>. It might work with your compiler, but it won't work for everyone.Break this sort of statement up into separate assignments: SV *s; STRLEN len; char * ptr; ptr = SvPV(s, len); foo(ptr, len);If you want to know if the scalar value is TRUE, you can use: SvTRUE(SV*)Although Perl will automatically grow strings for you, if you need to forcePerl to allocate more memory for your SV, you can use the macro SvGROW(SV*, STRLEN newlen)which will determine if more memory needs to be allocated. If so, it willcall the function C<sv_grow>. Note that C<SvGROW> can only increase, notdecrease, the allocated memory of an SV and that it does not automaticallyadd a byte for the a trailing NUL (perl's own string functions typically doC<SvGROW(sv, len + 1)>).If you have an SV and want to know what kind of data Perl thinks is storedin it, you can use the following macros to check the type of SV you have. SvIOK(SV*) SvNOK(SV*) SvPOK(SV*)You can get and set the current length of the string stored in an SV withthe following macros: SvCUR(SV*) SvCUR_set(SV*, I32 val)You can also get a pointer to the end of the string stored in the SVwith the macro: SvEND(SV*)But note that these last three macros are valid only if C<SvPOK()> is true.If you want to append something to the end of string stored in an C<SV*>,you can use the following functions: void sv_catpv(SV*, const char*); void sv_catpvn(SV*, const char*, STRLEN); void sv_catpvf(SV*, const char*, ...); void sv_catpvfn(SV*, const char*, STRLEN, va_list *, SV **, I32, bool); void sv_catsv(SV*, SV*);The first function calculates the length of the string to be appended byusing C<strlen>. In the second, you specify the length of the stringyourself. The third function processes its arguments like C<sprintf> andappends the formatted output. The fourth function works like C<vsprintf>.You can specify the address and length of an array of SVs instead of theva_list argument. The fifth function extends the string stored in the firstSV with the string stored in the second SV. It also forces the second SVto be interpreted as a string.The C<sv_cat*()> functions are not generic enough to operate on values thathave "magic". See L<Magic Virtual Tables> later in this document.If you know the name of a scalar variable, you can get a pointer to its SVby using the following: SV* get_sv("package::varname", FALSE);This returns NULL if the variable does not exist.If you want to know if this variable (or any other SV) is actually C<defined>,you can call: SvOK(SV*)The scalar C<undef> value is stored in an SV instance called C<PL_sv_undef>. Itsaddress can be used whenever an C<SV*> is needed.There are also the two values C<PL_sv_yes> and C<PL_sv_no>, which contain BooleanTRUE and FALSE values, respectively. Like C<PL_sv_undef>, their addresses canbe used whenever an C<SV*> is needed.Do not be fooled into thinking that C<(SV *) 0> is the same as C<&PL_sv_undef>.Take this code: SV* sv = (SV*) 0; if (I-am-to-return-a-real-value) { sv = sv_2mortal(newSViv(42)); } sv_setsv(ST(0), sv);This code tries to return a new SV (which contains the value 42) if it shouldreturn a real value, or undef otherwise. Instead it has returned a NULLpointer which, somewhere down the line, will cause a segmentation violation,bus error, or just weird results. Change the zero to C<&PL_sv_undef> in the firstline and all will be well.To free an SV that you've created, call C<SvREFCNT_dec(SV*)>. Normally thiscall is not necessary (see L<Reference Counts and Mortality>).=head2 OffsetsPerl provides the function C<sv_chop> to efficiently remove charactersfrom the beginning of a string; you give it an SV and a pointer tosomewhere inside the the PV, and it discards everything before thepointer. The efficiency comes by means of a little hack: instead ofactually removing the characters, C<sv_chop> sets the flag C<OOK>(offset OK) to signal to other functions that the offset hack is ineffect, and it puts the number of bytes chopped off into the IV fieldof the SV. It then moves the PV pointer (called C<SvPVX>) forward thatmany bytes, and adjusts C<SvCUR> and C<SvLEN>. Hence, at this point, the start of the buffer that we allocated livesat C<SvPVX(sv) - SvIV(sv)> in memory and the PV pointer is pointinginto the middle of this allocated storage.This is best demonstrated by example: % ./perl -Ilib -MDevel::Peek -le '$a="12345"; $a=~s/.//; Dump($a)' SV = PVIV(0x8128450) at 0x81340f0 REFCNT = 1 FLAGS = (POK,OOK,pPOK) IV = 1 (OFFSET) PV = 0x8135781 ( "1" . ) "2345"\0 CUR = 4 LEN = 5Here the number of bytes chopped off (1) is put into IV, andC<Devel::Peek::Dump> helpfully reminds us that this is an offset. Theportion of the string between the "real" and the "fake" beginnings isshown in parentheses, and the values of C<SvCUR> and C<SvLEN> reflectthe fake beginning, not the real one.Something similar to the offset hack is perfomed on AVs to enableefficient shifting and splicing off the beginning of the array; whileC<AvARRAY> points to the first element in the array that is visible fromPerl, C<AvALLOC> points to the real start of the C array. These areusually the same, but a C<shift> operation can be carried out byincreasing C<AvARRAY> by one and decreasing C<AvFILL> and C<AvLEN>.Again, the location of the real start of the C array only comes intoplay when freeing the array. See C<av_shift> in F<av.c>.=head2 What's Really Stored in an SV?Recall that the usual method of determining the type of scalar you have isto use C<Sv*OK> macros. Because a scalar can be both a number and a string,usually these macros will always return TRUE and calling the C<Sv*V>macros will do the appropriate conversion of string to integer/double orinteger/double to string.If you I<really> need to know if you have an integer, double, or stringpointer in an SV, you can use the following three macros instead: SvIOKp(SV*) SvNOKp(SV*) SvPOKp(SV*)These will tell you if you truly have an integer, double, or string pointerstored in your SV. The "p" stands for private.In general, though, it's best to use the C<Sv*V> macros.=head2 Working with AVsThere are two ways to create and load an AV. The first method creates anempty AV: AV* newAV();The second method both creates the AV and initially populates it with SVs: AV* av_make(I32 num, SV **ptr);The second argument points to an array containing C<num> C<SV*>'s. Once theAV has been created, the SVs can be destroyed, if so desired.Once the AV has been created, the following operations are possible on AVs: void av_push(AV*, SV*); SV* av_pop(AV*); SV* av_shift(AV*); void av_unshift(AV*, I32 num);These should be familiar operations, with the exception of C<av_unshift>.This routine adds C<num> elements at the front of the array with the C<undef>value. You must then use C<av_store> (described below) to assign valuesto these new elements.Here are some other functions: I32 av_len(AV*); SV** av_fetch(AV*, I32 key, I32 lval); SV** av_store(AV*, I32 key, SV* val);The C<av_len> function returns the highest index value in array (justlike $#array in Perl). If the array is empty, -1 is returned. TheC<av_fetch> function returns the value at index C<key>, but if C<lval>is non-zero, then C<av_fetch> will store an undef value at that index.The C<av_store> function stores the value C<val> at index C<key>, and doesnot increment the reference count of C<val>. Thus the caller is responsiblefor taking care of that, and if C<av_store> returns NULL, the caller willhave to decrement the reference count to avoid a memory leak. Note thatC<av_fetch> and C<av_store> both return C<SV**>'s, not C<SV*>'s as theirreturn value. void av_clear(AV*); void av_undef(AV*); void av_extend(AV*, I32 key);The C<av_clear> function deletes all the elements in the AV* array, butdoes not actually delete the array itself. The C<av_undef> function willdelete all the elements in the array plus the array itself. TheC<av_extend> function extends the array so that it contains at least C<key+1>elements. If C<key+1> is less than the currently allocated length of the array,then nothing is done.If you know the name of an array variable, you can get a pointer to its AVby using the following:
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?